我正在研究一個程序,它會詢問用戶一個整數,然後另一個,並把它們作爲圖點。它會告訴他們之間的距離。用戶必須輸入一個整數或按「Q」退出。如果是其他內容(不是整數或字母「Q」),它會告訴他們這是不正確的,請再試一次。我認爲這是我可以做到的,但它會返回錯誤cannot find symbol
。幫助非常感謝!詢問整數作爲輸入並退出如果q但如果任何其他字母再試
import java.util.Scanner;
public class PointCheck{
public static void main(String[] args){
try {
System.out.println("Welcome");
System.out.println("To quit at anytime please type \"Q\". Enter point:");
char Q = 'Q';
char q = 'q';
Scanner scan = new Scanner (System.in);
input = scan.next();
while (input.hasNext()) {
if (input.hasNextInt()) {
System.out.println("Int input");
}
else if (input.equals("Q")) {
System.out.println("Exiting");
}
/*else {
System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");
}*/
}
}
catch(Exception e){
System.out.println("Exiting Program.");
}
}
}
如果我不註釋掉最後一個else語句,它只是默認爲那個,並且永遠循環我的錯誤信息。
在行'input = scan.next()'輸入沒有聲明的類型。在此之後,您正嘗試使用'input'而不是'scan'作爲'Scanner'。 – Kevin
你沒有聲明輸入,如果你將它定義爲字符串比hasnext和hasintnext不會與它合作 –