我的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");
}
答案 0 :(得分:1)
对于这些类型的程序,您可以使用do-while循环。这是第一次没有条件运行,然后你可以在userInput变量中获得用户的输入。
.text-div {
position: relative;
top: 50%;
transform: translateY(-50%);
display: table;
}
答案 1 :(得分:1)
我认为问题是你在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");
}
答案 2 :(得分:0)
你永远不会离开你的循环,因为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");
这样,而用户输入"是",当他输入其他内容时,您将进入循环并离开。如果用户输入的内容不是"是"从一开始,你就永远不会进入循环并立即关闭应用程序。
答案 3 :(得分:0)
工作代码:请检查代码
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循环中的用户获取数据。还要检查代码 评论更多信息。
答案 4 :(得分:-1)
最重要的问题是,在第一次检查后,您永远不会更改userInput的值。所以无论他们是否输入"是"或"否"在循环结束时,它将保持循环。当您询问是否要进入另一位患者时,您甚至不会让用户选择在循环结束时输入任何内容。其次你应该检查他们是否输入了'是,是,是,是"使用a降低功能