Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.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中的While循环问题_Java_Loops_While Loop - Fatal编程技术网

Java中的While循环问题

Java中的While循环问题,java,loops,while-loop,Java,Loops,While Loop,我的while循环有问题。第三次执行后,它不会接受用户输入,只输出“输入您的名字”。我试过很多方法,但都不管用。循环应继续运行,直到用户键入“否” 我知道我很接近 System.out.print("Would you like to enter another patient? Type Yes or No: "); String userInput = input.nextLine(); if (userInput.equals("Yes")) { while (userInput

我的while循环有问题。第三次执行后,它不会接受用户输入,只输出“输入您的名字”。我试过很多方法,但都不管用。循环应继续运行,直到用户键入“否”

我知道我很接近

System.out.print("Would you like to enter another patient? Type Yes or No: ");
String userInput = input.nextLine();
if (userInput.equals("Yes")) 
{
    while (userInput.equals("Yes")) 
    {
        System.out.print("Enter your first name: ");
        String firstName2 = input.nextLine();
        System.out.print("Enter your last name: ");
        String lastName2 = input.nextLine();
        System.out.print("Enter your address: ");
        String address2 = input.nextLine();
        System.out.print("Enter your city: ");
        String city2 = input.nextLine();
        System.out.print("Enter your state: ");
        String state2 = input.nextLine();
        System.out.print("Enter your zip code + 4: ");
        String zip2 = input.nextLine();
        System.out.print("Enter amount owed: ");
        String amount2 = input.nextLine();
        System.out.print("Enter your payment amount: ");
        String payment2 = input.nextLine();
        System.out.print("Enter the date of payment: ");
        String date2 = input.nextLine();
        System.out.println("\t\t\t\t\t" + "XYZ Hospital");
        System.out.println();
        System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
        System.out.println();
        System.out.println("Last" + "\t" + "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
        System.out.println();
        System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 + "\t\t" + date2);
        System.out.print("Would you like to enter another patient? Type Yes or No: ");
    }
}
else if (userInput.equals("No")) 
{
    System.out.println("Goodbye");
}

您永远不会退出循环,因为
userInput
仅在循环之前设置一次,并且从未在循环内部设置,这意味着您永远不会更改
while
条件

System.out.print("Would you like to enter another patient? Type Yes or No: ");
String userInput = input.nextLine();
if (userInput.equals("Yes")) {
    while (userInput.equals("Yes")) {
        //your data entry...

        System.out.print("Would you like to enter another patient? Type Yes or No: ");
        // change userInput here, else, you're never going out of the loop.
        // so add this
        userInput = input.nextLine();
    }
} else if (userInput.equals("No")) {
    System.out.println("Goodbye");
}
尽管如此,您的解决方案可能会更符合您的需求:

System.out.print("Would you like to enter another patient? Type Yes or No: ");
String userInput = input.nextLine();
while (userInput.equals("Yes")) {
    //your data entry...

    System.out.print("Would you like to enter another patient? Type Yes or No: ");
    userInput = input.nextLine();
}

System.out.println("Goodbye");

这样,当用户输入“是”时,您将进入循环,当用户输入其他内容时退出。如果用户从一开始就输入了“是”以外的内容,则您永远不会进入循环并立即关闭应用程序。

最大的问题是,在第一次检查后,您永远不会更改userInput的值。因此,无论他们在循环末尾键入“是”或“否”,它都将继续循环。当您询问用户是否要输入其他患者时,您甚至从未给用户在循环结束时输入任何内容的选项。其次,您可能应该检查他们是否输入了“yes,yes,yes,y”,使用一个to lower函数

来降低您可以使用do while循环的这些类型的程序。它第一次无条件运行,然后您可以在userInput变量中从用户获取输入

String userInput;
 do{
    System.out.print("Enter your first name: ");
    String firstName2 = input.nextLine();
    System.out.print("Enter your last name: ");
    String lastName2 = input.nextLine();
    System.out.print("Enter your address: ");
    String address2 = input.nextLine();
    System.out.print("Enter your city: ");
    String city2 = input.nextLine();
    System.out.print("Enter your state: ");
    String state2 = input.nextLine();
    System.out.print("Enter your zip code + 4: ");
    String zip2 = input.nextLine();
    System.out.print("Enter amount owed: ");
    String amount2 = input.nextLine();
    System.out.print("Enter your payment amount: ");
    String payment2 = input.nextLine();
    System.out.print("Enter the date of payment: ");
    String date2 = input.nextLine();
    System.out.println("\t\t\t\t\t" + "XYZ Hospital");
    System.out.println();
    System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
    System.out.println();
    System.out.println("Last" + "\t" + "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
    System.out.println();
    System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 + "\t\t" + date2);
    System.out.print("Would you like to enter another patient? Type Yes or No: ");
    userInput = input.nextLine();

}while (userInput.equals("Yes"));

我认为问题在于您正在if语句中执行while循环。尝试在循环中插入if,如下所示:

    Scanner input=new Scanner(System.in);
    System.out.println("enter details");
    String answer=input.next();
    while(answer.equals("yes")) {
        if(answer.equals("yes")) {
System.out.println("name");
String name=input.next();
System.out.println("last name");
String last=input.next();
System.out.println("enter details");
     answer=input.next();    
} }

System.out.println("bye");

    }

工作代码:请检查代码

import java.util.Scanner;

public class temp {
    static String userInput;
    private static Scanner input;

    public static void main(String[] args) {
        input = new Scanner(System.in); 

        System.out
                .print("Would you like to enter another patient? Type Yes or No:  ");
        userInput = input.nextLine();

        while (userInput.equals("Yes")) { //while loop will iterate till "userInput is equals to Yes"
            System.out.print("Enter your first name: ");
            String firstName2 = input.nextLine();
            System.out.print("Enter your last name: ");
            String lastName2 = input.nextLine();
            System.out.print("Enter your address: ");
            String address2 = input.nextLine();
            System.out.print("Enter your city: ");
            String city2 = input.nextLine();
            System.out.print("Enter your state: ");
            String state2 = input.nextLine();
            System.out.print("Enter your zip code + 4: ");
            String zip2 = input.nextLine();
            System.out.print("Enter amount owed: ");
            String amount2 = input.nextLine();
            System.out.print("Enter your payment amount: ");
            String payment2 = input.nextLine();
            System.out.print("Enter the date of payment: ");
            String date2 = input.nextLine();
            System.out.println("\t\t\t\t\t" + "XYZ Hospital");
            System.out.println();
            System.out.println("Name Information" + "\t\t\t\t" + "Address"
                    + "\t\t\t\t\t\t" + "Payment");
            System.out.println();
            System.out.println("Last" + "\t" + "First" + "\t\t\t"
                    + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t"
                    + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount"
                    + "\t" + "Payment Date");
            System.out.println();
            System.out.println(lastName2 + " " + firstName2 + "\t" + address2
                    + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2
                    + "\t\t" + payment2 + "\t\t" + date2);
            System.out
                    .print("Would you like to enter another patient? Type Yes or No: \n");
            userInput = input.nextLine(); // Taking user choice for "Yes" or "No"

            if (userInput.equals("No")) {
                break; // will Exit loop when user enter "No"
            }
        }

        if (userInput.equals("No")) {
            System.out.println("Goodbye");
        } else {
            System.out.println(" Invalid input :("); // will print when user enter anything other than "Yes" or "No"

        }
    }
}
完成后,您没有再次从用户处获取输入 在while循环中从用户获取数据。还要检查代码 更多信息的评论


在循环结束时再次请求用户输入,或在循环结束时将其放入do。如果循环没有任何意义,
userInput
在循环内从未被修改过。右上第四行完整、难看的代码。不可读。难怪你有问题。您需要在内部循环中设置另一个userInput。不,您在最后没有请求用户输入。查看您的代码以及执行的频率
userInput=input.nextLine()。就一次,那超出了你的范围谢谢!我知道这很简单。@mike,不客气。根据您的需要,您可能还需要执行
do while
循环。它允许您至少进入一次循环,并询问用户是否只想在代码中的一个位置继续。