[英]New To Java, Can't figure out to use a while loop to end user input
我是 Java 新手,我目前正在嘗試創建一個 while 循環,如果用戶輸入 Yes,它將循環。 我也希望它提示用戶輸入“endinput”,如果他們改為放置停止。 有任何想法嗎? 剛剛開始 Java 編程,這讓我很難過,因為我不知道如何繼續。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[]
title=new String[100],
author=new String[100],
publisher=new String[100],
ISBN=new String[100],
endinput=new String[100];
boolean Stop = false;
boolean Yes = true;
double[] price=new double[100];
System.out.println("Welcome To Kieran's Bookstore");
System.out.println("Input The Title:");
title[0] = scan.next();
System.out.println("Input The Author:");
author[0] = scan.next();
System.out.println("Input The Price Of The Book:");
scan.next();
System.out.println("Input The Publisher:");
publisher[0]= scan.next();
System.out.println("Input The ISBN:");
ISBN[0]=scan.next();
System.out.println("Would you like to continue?(Yes/Stop)");
scan.next();
while(!Stop)
{
System.out.println("Input The Title:");
title[0] = scan.next();
System.out.println("Input The Author:");
author[0] = scan.next();
System.out.println("Input The Price Of The Book:");
scan.next();
System.out.println("Input The Publisher:");
publisher[0]= scan.next();
System.out.println("Input The ISBN:");
ISBN[0]=scan.next();
System.out.println("Would you like to continue?(Yes/Stop)");
scan.equals(Stop);
}
while (!Stop) {System.out.println("Please Type 'endinput':");
scan.next();
}
System.out.println("Please Type 'endinput':");
scan.next();
if (scan.equals("endinput"))System.exit(0);
scan.close();
您只需要使用一段while
即可實現這一目標。 為scan.next()
分配一個變量並使用if
進行檢查。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] title = new String[100], author = new String[100], publisher = new String[100], ISBN = new String[100],
endinput = new String[100];
// boolean Stop = false;
boolean Yes = true;
double[] price = new double[100];
while (Yes) {
System.out.println("Welcome To Kieran's Bookstore");
System.out.println("Input The Title:");
title[0] = scan.next();
System.out.println("Input The Author:");
author[0] = scan.next();
System.out.println("Input The Price Of The Book:");
scan.next();
System.out.println("Input The Publisher:");
publisher[0] = scan.next();
System.out.println("Input The ISBN:");
ISBN[0] = scan.next();
System.out.println("Would you like to continue?(Yes/Stop)");
String ans = scan.next();
if (ans.equals("endinput") || (ans.equals("Stop"))) {
Yes = false;
System.exit(0);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.