如何循环这个if语句?

How to loop this if statement?

基本上我是在寻找我的问题。我的 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 class,就是这样:

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 语句。 我现在要做的是按照"friend"的integer/age对链表进行排序,这样打印链表的时候就可以按照从小到大的顺序排列了。如果有人能给我指出正确的方向,那就太棒了!

总之,谢谢大家的帮助,希望对遇到的人有所帮助。

虽然您也可以使用 do-while,但在这里使用 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();
   }     

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

当您希望无论条件为真还是假都至少执行一次任务时,请使用do-while循环。 do-while 循环的结构是-

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

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

while(condition){
   //do something
}

如前所述,"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());
 }
}
}
 

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

祝一切顺利。

请务必让我知道结果:)

祝你好运!