Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何循环这个if语句?_Java_Loops_For Loop_While Loop_Linked List - Fatal编程技术网

Java 如何循环这个if语句?

Java 如何循环这个if语句?,java,loops,for-loop,while-loop,linked-list,Java,Loops,For Loop,While Loop,Linked List,基本上我是在寻找我的问题。我的课程看起来像这样: 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.pr

基本上我是在寻找我的问题。我的课程看起来像这样:

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循环

导入java.io.*; 导入java.util.*; 公务舱{ 公共静态字符串参数[]{ LinkedList addressBook=新建LinkedList; 扫描仪输入=新的ScannerSystem.in; int n=0; 做{ System.out.println是否要添加朋友?例如Y或N; 字符串reply=input.nextLine; 如果回答是相等的{ 你朋友叫什么名字?; 字符串名称=input.nextLine; 你朋友几岁?; int age=input.nextInt; Friend newFriend=新朋友姓名、年龄; addbook.addName:+newFriend.name+;+Age:+newFriend.Age; System.out.println这是到目前为止您的地址簿:+addressBook; n++; }如果回答为,则为else.equalsN{ 谢谢你抽出时间; System.out.println您想知道您的通讯簿中有谁吗?比如Y或N; 字符串userinput=input.nextLine; 如果回答是相等的{ System.out.println这是到目前为止您的地址簿:+addressBook; 系统输出打印再见!; }如果回答为,则为else.equalsN{ System.out.printlnook!再见!; } n=101; } }n<100; }
} 在这里使用while循环似乎是最好的选择,尽管您也可以使用do-while。但是如果使用do while,即使reply变量的第一个输入不是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的条件。循环中的代码执行次数与为reply变量输入Y的次数相同。

在这里使用while循环似乎是最好的选择,尽管您也可以使用do while。但是如果使用do while,即使reply变量的第一个输入不是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的条件。循环中的代码执行次数与为reply变量输入Y的次数相同。

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

do{
   //do something
}while(condition);    
while(condition){
   //do something
}
否则使用while循环-如果条件为true,则执行循环。while循环的结构是-

do{
   //do something
}while(condition);    
while(condition){
   //do something
}
无论条件是真是假,如果您希望至少执行一次任务,请使用do while循环。do while循环的结构为-

do{
   //do something
}while(condition);    
while(condition){
   //do something
}
否则使用while循环-如果条件为true,则执行循环。while循环的结构是-

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

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

如果你想了解更多这方面的信息,这是一个好去处

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

导入java.io.*; 导入java.io.BufferedReader; 导入java.util.*; 公务舱{ 公共静态无效主字符串参数[] { 尝试 { LinkedList addressBook=新建LinkedList; BufferedReader br=新的BufferedReadernew InputStreamReaderSystem.in; System.out.println是否要添加朋友?例如Y或N; 字符串reply=br.readLine; 布尔addMore=true; ifreply.equalsY{ addMore=true; }else ifreply.equalsN{ addMore=false; } 何去何从 { 你朋友叫什么名字?; 字符串名称=br.readLine; 你朋友几岁?; int age=Integer.parseIntbr.readLine; Friend newFriend=新朋友姓名、年龄; addbook.addName:+newFriend.name+;+Age:+newFriend.Age; System.out.println这是到目前为止您的地址簿:+addressBook; System.out.println是否要添加朋友?例如Y或N; reply=br.readLine; ifreply.equalsY{ addMore=true; }e lse ifreply.equalsN{ 谢谢你抽出时间; addMore=false; } } 谢谢你抽出时间; }捕捉异常{ System.out.printlne.getMessage; } } }
如前所述,do-while循环和while循环之间的区别在于do-while循环在循环的底部执行条件,这反过来意味着至少要执行一次代码

如果你想了解更多这方面的信息,这是一个好去处

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

导入java.io.*; 导入java.io.BufferedReader; 导入java.util.*; 公务舱{ 公共静态无效主字符串参数[] { 尝试 { LinkedList addressBook=新建LinkedList; BufferedReader br=新的BufferedReadernew InputStreamReaderSystem.in; System.out.println是否要添加朋友?例如Y或N; 字符串reply=br.readLine; 布尔addMore=true; ifreply.equalsY{ addMore=true; }else ifreply.equalsN{ addMore=false; } 何去何从 { 你朋友叫什么名字?; 字符串名称=br.readLine; 你朋友几岁?; int age=Integer.parseIntbr.readLine; Friend newFriend=新朋友姓名、年龄; addbook.addName:+newFriend.name+;+Age:+newFriend.Age; System.out.println这是到目前为止您的地址簿:+addressBook; System.out.println是否要添加朋友?例如Y或N; reply=br.readLine; ifreply.equalsY{ addMore=true; }else ifreply.equalsN{ 谢谢你抽出时间; addMore=false; } } 谢谢你抽出时间; }捕捉异常{ System.out.printlne.getMessage; } } }
如果你能解释一下为什么或者什么循环会在另一个循环上起作用,那就太棒了。抱歉。现在编辑。我还不能对此进行表决,但我想感谢你的解释。如果你解释一下为什么或如何使用循环,那将是非常棒的。抱歉。现在编辑。我还不能就此投票,但我想感谢你的解释。谢谢!我做了一次编辑,显示了我的结果,尽管我没有使用您完成此任务的方法。我仍然会尝试,虽然我对这个还是很陌生。我只是看了一眼,做得好:。。。编程就是这样,解决任何问题的方法有一百零一种。很高兴我们能提供帮助。谢谢!我做了一次编辑,显示了我的结果,尽管我没有使用您完成此任务的方法。我仍然会尝试,虽然我对这个还是很陌生。我只是看了一眼,做得好:。。。编程就是这样,解决任何问题的方法有一百零一种。很高兴我们能提供帮助。谢谢!在我的编辑中,我展示了如何使用do while循环。谢谢!在我的编辑中,我展示了如何使用do-while循环。