2013-02-17 74 views
0

即時通訊編寫一個程序,比較兩個整數,我希望程序運行,直到用戶厭倦,想要退出,這樣做,用戶將輸入q退出。我想寫一個while循環,比較兩個字符,一個是用戶輸入的,另一個字符是'q'分配給它。然而,當我輸入q我的程序擊碎這是爲什麼,我該如何解決一個for循環運行/停止程序

import java.io.IOException; 
import java.util.Scanner; 


public class numComp { 

public static void main(String[] args) { 

    Scanner in = new Scanner(System.in); 
    int a, b; 
    char key = 'q'; 
    System.out.println("enter 'q' to quit program and any other key to start it"); 

    char btn = 0; 
    try { 
     btn = (char) System.in.read(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    while (btn != key){ 
    System.out.println("enter two numbers to compare them"); 
    System.out.println("please enter first number:"); 
    a = in.nextInt(); 
    System.out.println("please enter second number:"); 
    b = in.nextInt(); 

    if (a==b) 
     System.out.println("the numbers you entered are the same"); 
    else if (a>b) 
     System.out.println("your first number is greater then your second number by "+ (a-b)); 
    else 
     System.out.println("your first number is smaller then your second number by "+ (b-a)); 
    } 

} 

} 
+1

而用戶應該在哪裏輸入'q'?導致我看到它的方式,你只允許輸入數字。 – 2013-02-17 10:21:35

回答

0

您需要更改btn當你選擇了它。 您在循環之前設置它只有一次,和因爲

a = in.nextInt(); 

您嘗試讀取int當你進入一個char從不更新。

唯一的例外是。

您需要在環路類似的末尾添加:

btn = (char) System.in.read(); 
1

您應將循環是這樣的:

try{ 
    btn = (char) System.in.read(); 
    while (btn != key){ 
    System.out.println("enter two numbers to compare them"); 
    System.out.println("please enter first number:"); 
    a = in.nextInt(); 
    System.out.println("please enter second number:"); 
    b = in.nextInt(); 

    if (a==b) 
     System.out.println("the numbers you entered are the same"); 
    else if (a>b) 
     System.out.println("your first number is greater then your second number by "+ (a-b)); 
    else 
    System.out.println("your first number is smaller then your second number by "+ (b-a)); 

    System.out.println("enter 'q' to quit program and any other key to start it again"); 
    btn = (char) System.in.read(); 
    } // END OF WHILE LOOP 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

另外請注意,您需要再次獲得來自輸入在while循環結束之前的用戶。

+0

它現在有效。謝謝 – cloud9resident 2013-02-17 10:48:02

+0

歡迎您! – Abubakkar 2013-02-17 11:04:58

1

這是因爲在while循環中,您正在掃描整數,但「q」是一個字符。您需要讓用戶有機會在裏面讀取btnwhile循環。

0

這是簡化的程序。

public static void main(String[] args){ 
    // Scanner 
    Scanner s = new Scanner(System.in); 
    // Enter the program 
    System.out.println("enter 'q' to quit program and any other key to start it"); 
    int a, b; 
    try { 
     char key; 
     while ((key = s.next().charAt(0))!='q'){ 
      System.out.println("enter two numbers to compare them"); 
      System.out.println("please enter first number:"); 
      a = s.nextInt(); 
      System.out.println("please enter second number:"); 
      b = s.nextInt(); 

      if (a==b) 
       System.out.println("the numbers you entered are the same"); 
      else if (a>b) 
       System.out.println("your first number is greater then your second number by "+ (a-b)); 
      else 
       System.out.println("your first number is smaller then your second number by "+ (b-a)); 
     } 
    } catch (Exception e){ e.printStackTrace(); } 
}