[英]While loop problems in Java
我的 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");
}
對於這些類型的程序,您可以使用 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");
}
你永遠不會離開你的循環,因為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");
這樣,當用戶輸入“是”時,您將進入循環並在他輸入其他內容時退出。 如果用戶從一開始就輸入了“是”以外的其他內容,您將永遠不會進入循環並立即關閉應用程序。
工作代碼:請檢查代碼
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 循環內完成從用戶那里獲取數據,您就不會再次從用戶那里獲取輸入。 另請查看代碼注釋以獲取更多信息。
嗯,最大的問題是在第一次檢查后你永遠不會改變 userInput 的值。 所以無論他們在循環結束時輸入“是”還是“否”,它都會繼續循環。 當您詢問他們是否想輸入另一名患者時,您甚至從未讓用戶選擇在循環結束時輸入任何內容。 其次,您可能應該檢查他們是否使用降低功能輸入了“是,是,是,是,y”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.