【问题标题】:How to loop this if statement?如何循环这个 if 语句?
【发布时间】:2015-04-14 19:27:03
【问题描述】:

基本上我正在寻找我的问题。我的ABookclass 看起来像这样:

import java.io.*;
import java.util.*;

public class ABook
{
   public static void main (String args[])
   {
       LinkedList addressBook = new LinkedList();
       Scanner input = new Scanner(System.in);
       System.out.println("Would you like to add a friend? (Say Y or N)"); 
       String reply = input.nextLine();
       if(reply.equals("Y"))
       {
           System.out.println("What is the name of your friend?");
           String name = input.nextLine();
           System.out.println("What is the age of your friend?");
           int age = input.nextInt();
           Friend newFriend = new Friend(name,age);
           addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);
           System.out.println("This is your Address Book so far: " + addressBook);
       }     
       else if(reply.equals("N")){
           System.out.println("Thank you for your time");
       }
    }
}

如果你需要我在这里使用的 Friend 类,就是这样:

public class Friend
{
    public String name;
    public int age;
    public Friend(String n, int a)
    {   
        name = n;
        age = a;
    }
}

我很不确定我是否使用“do”或“while”循环或如何使用它。如果您解释了为什么或如何循环在另一个循环上工作,那就太棒了。

谢谢。

编辑:在我发布这个问题之前,我并没有意识到这个社区有多活跃,而且我认为我不会像在这么短的时间。所以,在我看到你的回复之前,我想出了自己的方法来循环它,方法是使用一个看起来像这样的 do-while 循环。

import java.io.*;
import java.util.*;

public class ABook {
  public static void main(String args[]) {
    LinkedList addressBook = new LinkedList();
    Scanner input = new Scanner(System.in);
    int n = 0;
    do {
      System.out.println("Would you like to add a friend? (Say Y or N)");
      String reply = input.nextLine();
      if (reply.equals("Y")) {
        System.out.println("What is the name of your friend?");
        String name = input.nextLine();
        System.out.println("What is the age of your friend?");
        int age = input.nextInt();
        Friend newFriend = new Friend(name, age);
        addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);

        System.out.println("This is your Address Book so far: " + addressBook);
        n++;
      } else if (reply.equals("N")) {
        System.out.println("Thank you for your time");
        System.out.println("Would you like to know who is in your Address Book? (Say Y or N)");
        String userinput = input.nextLine();
        if (reply.equals("Y")) {
          System.out.println("This is your Address Book so far: " + addressBook);
          System.out.println("Goodbye!");
        } else if (reply.equals("N")) {
          System.out.println("Okay! Goodbye!");
        }
        n = 101;
      }
    } while (n < 100);
  }
}

它运作良好,我什至必须在其中添加另一个 if - else 语句。 我现在要做的是按“朋友”的整数/年龄对链表进行排序,所以当我打印链表时,它可以按从最小到最旧的顺序排列。如果有人能指出我正确的方向,那就太棒了!

无论如何,感谢所有来帮助我的人,我希望这可以帮助其他遇到寻找答案的人。

【问题讨论】:

标签: java loops for-loop while-loop linked-list


【解决方案1】:

当你想至少执行一次任务时使用do-while循环,无论条件是真还是假。 do-while循环的结构是-

do{
   //do something
}while(condition);    

否则使用while 循环 - 如果条件为真,则执行循环。 while循环的结构是-

while(condition){
   //do something
}

【讨论】:

  • 谢谢!在我的编辑中,我展示了我是如何使用 do-while 循环的。
【解决方案2】:

如前所述,“do while”和“while”循环之间的区别在于,do-while 循环执行循环底部的条件,这反过来意味着将至少执行一次代码。

如果您想了解更多相关信息,this 是一个不错的去处。

就您的代码而言。您应该使用“while”循环,使用布尔值作为用户是否要继续添加的标志。您的代码最终将如下所示:


import java.io.*;
import java.io.BufferedReader;
import java.util.*;

public class ABook{
public static void main (String args[])
{
	try
	{
		LinkedList addressBook = new LinkedList();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
		System.out.println("Would you like to add a friend? (Say Y or N)"); 
		String reply = br.readLine();
		boolean addMore = true;
		if(reply.equals("Y")){
			addMore = true;
		}else if(reply.equals("N")){
			addMore = false;
		}
		while(addMore)
		{
			System.out.println("What is the name of your friend?");
			String name = br.readLine();
			System.out.println("What is the age of your friend?");
			int age = Integer.parseInt(br.readLine());
			Friend newFriend = new Friend(name,age);
			addressBook.add("Name: " + newFriend.name + "; " + "Age: " +  newFriend.age);
			System.out.println("This is your Address Book so far: " + addressBook);
			System.out.println("Would you like to add a friend? (Say Y or N)"); 
			reply = br.readLine();
			if(reply.equals("Y")){
				addMore = true;
			}else if(reply.equals("N")){
				System.out.println("Thank you for your time");
				addMore = false;
			}
		}
		System.out.println("Thank you for your time");
	}catch (IOException e) {
		System.out.println(e.getMessage());
	}
}
}
 

这应该可以解决并回答您的问题。请注意,“扫描仪”功能不允许暂停输入,因此无法满足您的需要。

一切顺利。

请让我知道结果:)

祝你好运!

【讨论】:

  • 谢谢!我做了一个显示我的结果的编辑,即使我没有使用你完成这个任务的方法。我仍然会尝试一下,虽然我对此还是很陌生。
  • 我刚刚看了看,干得好:)! ...编程就是这样,有一百零一种方法可以解决任何问题。很高兴我们能提供帮助。
【解决方案3】:

使用 while 循环似乎是最好的选择,尽管您也可以使用 do-while。但是,如果您使用 do-while,即使回复变量的第一个输入不是“Y”,它也会至少运行一次循环块。

String reply = input.nextLine();
   while(reply.equals("Y"))
   {
    System.out.println("What is the name of your friend?");
    String name = input.nextLine();
    System.out.println("What is the age of your friend?");
    int age = input.nextInt();
    Friend newFriend = new Friend(name,age);
    addressBook.add("Name: " + newFriend.name + "; " + "Age: " + newFriend.age);
    System.out.println("This is your Address Book so far: " + addressBook);
    System.out.println("Would you like to add a friend? (Say Y or N)"); 
    String reply = input.nextLine();
   }     

解释:如果回复变量的第一个输入是“Y”,则控件将进入 while 循环。最后一行再次提示输入,然后再次检查条件。循环内的代码将执行与为回复变量输入“Y”一样多次。

【讨论】:

  • “如果你解释了为什么或如何哪个循环会在另一个循环上起作用,那就太棒了。”
  • 道歉。现已编辑。
  • 我还不能对此投票,但我想感谢你的解释。
猜你喜欢
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
相关资源
最近更新 更多