我最近發佈了一個關於掃描儀沒有提供預期結果的問題,並瞭解到我的問題是我沒有用.nextLine()
來刷掃描儀。我對我正在處理的一個程序感到困惑,因爲我正在沖洗掃描儀,但是當我測試我的程序時,如果 - 當提示輸入數字時 - 我輸入一個字符串,我會得到錯誤的輸出。它重複兩次相同的循環。Java:掃描儀輸出不佳
循環的頂部有對nextLine()的調用,而處理無效輸入的else塊(例如我輸入的字符串)也有對nextLine()的調用。但不知何故還是我越來越壞輸出
所以要具體,這裏與用戶輸入的壞輸出的樣品中大膽和斜體問題的輸出
進入左邊的值:
輸入操作者:-
輸入右手值:噸
無效的輸入
輸入操作(+ - * /或:
無效操作
輸入操作(+ - * /或:
的上面四行會自動吐出到控制檯。
這是代碼片斷,其中有一個很大的評論,其中的代碼是錯誤的。我只是發佈了問題所在的while塊,但由於while塊是程序的大部分,整個程序只比這個段大一些,所以我認爲最好是發佈它。
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Calculator{
public static void main(String[] args){
double leftHandVal = 0.0;
//Output Title & Instructions
System.out.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
System.out.print("!\t\t\t\t!\n");
System.out.print("!\t INSTRUCTIONS\t\t!\n");
System.out.print("!\t\t\t\t!\n");
System.out.print("! INPUT\t\tOUTPUT\t\t!\n");
System.out.print("! *******\t *********\t\t!\n");
System.out.print("! c or C\t\tClear\t\t!\n");
System.out.print("! q or Q\t\tQuit\t\t!\n");
System.out.print("! +\t\tAddition\t\t!\n");
System.out.print("! -\t\tSubtraction\t!\n");
System.out.print("! *\t\tMultiplication\t!\n");
System.out.print("! /\t\tDivision\t\t!\n");
System.out.print("!\t\t\t\t!\n");
System.out.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
while(true){
Scanner input = new Scanner(System.in);
char op = '\n';//(+, -, *, or /) will use in switch statement for their ascii decimal values
System.out.print("Enter the left-hand value: ");
//these blocks allow the code at the very bottom to not erroneously ask the user for extra input with hasNext() calls
if(input.hasNext("c") || input.hasNext("C")){//even though its unlikely for a user to clear so early...just in case
leftHandVal = 0.0;
}
else if(input.hasNext("q") || input.hasNext("Q")){//even though its unlikely for a user to quit so early...just in case
op = 113;//assign q for quit code
}
else if(input.hasNextDouble()){
leftHandVal = input.nextDouble();
/*
*
*BAD CODE INSIDE WHILE BELOW
*BAD CODE INSIDE WHILE BELOW
*BAD CODE INSIDE WHILE BELOW
*BAD CODE INSIDE WHILE BELOW
*
*/
while(true){
input.nextLine();
double rightHandVal = 0.0;
System.out.print("\nEnter operator (+ - * or/: ");
if(input.hasNext()){
op = input.next().charAt(0);
}
//if user wishes to cancel or quit on operator prompt, break out of inner while to access the clear and quit code
if(op == 99 || op == 67){
op = 99;
break;
}
else if(op == 113 || op == 81){
op = 113;
break;
}
else if((op != 43) && (op != 45) && (op != 42) && (op != 47)){//if invalid operator, restart inner while
System.out.print("Invalid Operator");
continue;
}
System.out.print("Enter the right-hand value: ");
if(input.hasNextDouble()){
rightHandVal = input.nextDouble();
switch(op){
case 43:
System.out.printf("%.3f + %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal + rightHandVal));
leftHandVal += rightHandVal;
break;
case 45:
System.out.printf("%.3f - %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal - rightHandVal));
leftHandVal -= rightHandVal;
break;
case 42:
System.out.printf("%.3f * %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal * rightHandVal));
leftHandVal *= rightHandVal;
break;
case 47:
System.out.printf("%.3f/%.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal/rightHandVal));
leftHandVal /= rightHandVal;
break;
}
}
//if clear or quit requested from prompt for right-hand value, break to reach the clear and quit code
else if(input.hasNext("c") || input.hasNext("C")){
op = 99;
break;
}
else if(input.hasNext("q") || input.hasNext("Q")){
op = 113;
break;
}
else{
System.out.print("Invalid Input");
}
}
}
//if c || C reset op to null and restart outer while
if(op == 99 || op == 67){
op = '\n';
leftHandVal = 0.0;
continue;
}
//else if q || Q, prompt user with a popup to confirm.
if(op == 113 || op == 81){
int response = JOptionPane.showConfirmDialog(null, "QUIT CALCULATOR?", null, JOptionPane.YES_NO_OPTION);
if(response == 0){
System.exit(0);
}
continue;
}
}
}
}
這裏有什麼問題? –
@BigAl說了些什麼,也許你應該首先嚐試評估角色,因爲角色是字符。這樣做可能不能解決你所遇到的問題,但是你現在正在做這件事的方式(評估爲整數)絕非任何方式。 – Radiodef
問題是爲什麼 - 當我在輸入右鍵值的提示中輸入字母時 - 即使我正在使用input.nextLine()刷新掃描儀,是否也獲得多個輸出。 – Justin