2017-12-03 48 views
1

我正在研究一個程序,它會詢問用戶一個整數,然後另一個,並把它們作爲圖點。它會告訴他們之間的距離。用戶必須輸入一個整數或按「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語句,它只是默認爲那個,並且永遠循環我的錯誤信息。

+0

在行'input = scan.next()'輸入沒有聲明的類型。在此之後,您正嘗試使用'input'而不是'scan'作爲'Scanner'。 – Kevin

+0

你沒有聲明輸入,如果你將它定義爲字符串比hasnext和hasintnext不會與它合作 –

回答

1

我使你的代碼進行一些修改,希望它能幫助:)

public class PointCheck { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    try { 

     System.out.println("Welcome"); 
     System.out.println("To quit at anytime please type \"Q\". Enter point:"); 
     Scanner scan = new Scanner(System.in); 

     while (scan.hasNext()) { 
      if (scan.hasNextInt()) { 
       System.out.println("Int input" + scan.nextInt()); 
      } else { 
       String input = scan.next(); 
       if (input.equalsIgnoreCase("Q")){ 
        System.out.println("Exiting"); 
        break; 

       }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."); 

    } 
} 

} 
+0

這是非常有用的。謝謝!我意識到我做錯了什麼:) –

0

你可以試試這個... 的投入將只需要整數,如果輸入以外的任何其他一個Integer,它將拋出一個異常並移動到程序將終止的catch塊。

import java.util.Scanner; 

public class demo { 
    public static void main(String[] args) { 
     System.out.println("Welcome"); 
     System.out.println("Press any key to exit.. Enter point:"); 
     Scanner scanner= new Scanner(System.in); 

     while (true){ 
      try { 
       int number = scanner.nextInt(); 
       System.out.println(number); 
      } 
      catch (Exception e){ 
       System.exit(0); 
      } 
     } 
    } 
} 
+0

OP不希望應用程序終止。請閱讀帖子。 – DevilsHnd

0

像這樣的東西可能是你喜歡的。閱讀代碼中的註釋:

System.out.println("Welcome"); 
Scanner scanner= new Scanner(System.in); 
int number = 0; 
while (true){ 
    System.out.println("Enter point (q to quit): "); 
    String strg = scanner.nextLine(); 
    // Get out of loop if q or Q or quit, 
    // or QUIT, or Quit, etc is entered. 
    // providing the input actually contains 
    // something. 
    if (!strg.equals("") && strg.toLowerCase().charAt(0) == 'q') { 
     break; 
    } 
    // Use the String.matches() method with regex to 
    // determine if an integer number was supplied. 
    if (!strg.matches("\\d+")) { 
     System.err.println("Invalid Entry - Integer values only! Try again.\n"); 
     continue; // Start loop from beginning. 
    } 
    // Convert string number to Integer. 
    number = Integer.parseInt(strg); 
    break; // Get outta loop  
} 
String msg = "The number entered is: " + number; 
if (number == 0) { msg = "Nothing Entered!"; } 
System.out.println(msg); 
相關問題