[英]Issues using scanner in java
我有一項任務,我們假設為書店創建一個程序,幫助他們估算自己的業務。 為此,我們假設要求用戶提供各種輸入,例如書籍的代碼,書籍的單一復制成本,當前的書籍數量,預期的課程注冊等等。 但是,當用戶輸入無效輸入時,我遇到問題,例如當預期注冊無效輸入時,程序開始從開始詢問問題而不是再次詢問相同問題。
任何人都可以幫我找出我做錯了什么? 這是我的代碼。
提前致謝。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to College Store Sale Class.");
System.out.println("Please enter all the info below as asked.");
System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. ");
double bookCost;
int bookAvailable;
String ending = sc.next();
int prosepectiveEnrollment;
String rOrO;
String nOrU;
while(!ending.equals("exit")){
System.out.print("Please enter the cost of book ($>=0). ");
bookCost = sc.nextDouble();
if(bookCost >= 0){
System.out.print("Please enter the number of books available (n>=0). ");
bookAvailable = sc.nextInt();
if(bookAvailable >= 0){
System.out.print("Please enter prospective class enrollement > 0: ");
prosepectiveEnrollment = sc.nextInt();
if(prosepectiveEnrollment > 0){
System.out.print("Is the book Required or Optional (ignoreCase)? ");
rOrO = sc.next();
if((rOrO.equalsIgnoreCase("Required") || rOrO.equalsIgnoreCase("Optional"))){
System.out.print("Is the book New or Used? Enter in 'N' or 'U' format. ");
nOrU = sc.next();
if((nOrU.equalsIgnoreCase("New") || nOrU.equalsIgnoreCase("Used"))){
System.out.println("Thanks for correct info, below is your result");
}
else{
System.out.println("(E) Please enter either 'New' or 'Used'. ");
}
}
else{
System.out.println("(D) Please enter either 'Required' or 'Optional'. ");
}
}
else{
System.out.println("(C) Number of prospective class enrollement must be > 0.");
}
}
else{
System.out.println("(B) Number of books should be >= 0.");
}
}
else{
System.out.println("(A) Cost should be >= 0. ");
}
}
if(ending.equalsIgnoreCase("exit"))
System.out.println("See ya later..!! Bbyee..");
sc.close();
}
你在循環中問問題。 當您的某個條件失敗時,程序將繼續超過所有后續條件(如果它們也不正確)並再次開始LOOP。 要糾正此問題,您可能會在循環內發生每個條件。 就像是
而(錯誤的條件回答):再問一次
要挑戰自己(並改進),請盡可能少地保留代碼!
祝你的編碼旅程好運:)
我試圖解決這個問題,也許你可以這樣做,讓我知道這是否適合你
public class Questionaire {
public static void main(String[] args) {
double bookCost = 0.0;
int bookAvailable = 0;
int prosepectiveEnrollment = 0;
String rOrO = "";
String nOrU = "";
try (Scanner sc = new Scanner(System.in)){
System.out.println("Welcome to College Store Sale Class.");
System.out.println("Please enter all the info below as asked.");
System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. ");
String ending = sc.next();
System.out.println("Your response "+ending);
while(!ending.equalsIgnoreCase("exit")){
bookCost = promptQuestion("Please enter the cost of book ($>=0).", param -> {
return param >= 0;
}, bookCost,sc);
System.out.println("Your response "+bookCost);
bookAvailable = promptQuestion("Please enter the number of books available (n>=0). ", param -> {
return param >= 0;
}, bookAvailable, sc);
System.out.println("Your response "+bookAvailable);
prosepectiveEnrollment = promptQuestion("Please enter prospective class enrollement > 0: ", param -> {
return param > 0;
}, prosepectiveEnrollment, sc);
System.out.println("Your response "+prosepectiveEnrollment);
rOrO = promptQuestion("Is the book Required or Optional (ignoreCase)? ", param -> {
return param.equalsIgnoreCase("Required") || param.equalsIgnoreCase("Optional");
}, rOrO, sc);
System.out.println("Your response "+rOrO);
nOrU = promptQuestion("Is the book New or Used? Enter in 'N' or 'U' format. ", param -> {
return (param.equalsIgnoreCase("New") || param.equalsIgnoreCase("Used")
|| param.equalsIgnoreCase("N") || param.equalsIgnoreCase("U"));
}, nOrU, sc);
System.out.println("Your response "+nOrU);
System.out.println("Write either 'Continue' or 'Exit' to continue or exit program respectively. ");
ending = sc.next();
System.out.println("Your response "+ending);
}
} catch (Exception e) {
e.printStackTrace();
}
}
static Integer promptQuestion(String question, Validation<Integer> validation, Integer input, Scanner sc) {
do {
System.out.println(question);
input = sc.nextInt();
} while(!validation.validate(input));
return input;
}
static Double promptQuestion(String question, Validation<Double> validation, Double input, Scanner sc) {
do {
System.out.println(question);
input = sc.nextDouble();
} while(!validation.validate(input));
return input;
}
static String promptQuestion(String question, Validation<String> validation, String input, Scanner sc) {
do {
System.out.println(question);
input = sc.next();
} while(!validation.validate(input));
return input;
}
@FunctionalInterface
private static interface Validation<T> {
boolean validate(T t);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.