2013-01-15 111 views
1

我對編程非常陌生,我寫了一個應用程序來處理某些形狀的區域。用掃描儀檢查用戶輸入的正確方法

我有AreaCalculations在另一個類可以提供如果需要,但工作正常。

我的問題是檢查用戶何時鍵入一個字符而不是雙。正如你從我的代碼中看到的,我通過使用while循環和(!reader.NextDouble())來使它工作。

這有效,但我不得不重複這個問題。我可以在整個程序中做到這一點,但有沒有更簡單/更整潔的方式?

感謝,

克雷格

到目前爲止我的代碼:

package areaprog; 

import java.util.Scanner; 

public class Mainprog { 


public static void main (String [] args){ 

    //Area Menu Selection 
    System.out.println("What shape do you need to know the area of?\n" + 
    "1: Square?\n" + 
    "2: Rectangle?\n" + 
    "3: Triangle?\n" + 
    "4: Circle? \n" + 
    "5: Exit\n"  
    ); 

    //User input for menu 

    Scanner reader = new Scanner(System.in); 
    System.out.println("Number: "); 

    //Menu syntax checking 
    while (!reader.hasNextDouble()) 
    { 
     System.out.println("Thats not a number you tool.\n"); 
     System.out.println("Now pick again\n" + 
       "1: Square?\n" + 
       "2: Rectangle?\n" + 
       "3: Triangle?\n" + 
       "4: Circle? \n" + 
       "5: Exit\n"  
       ); 

     reader.next(); //ask for next token?   
    }    
     double input = reader.nextDouble(); 
     reader.nextLine(); 



    //Depending on user selection, depends on what method is called using switch. 
Scanner scan = new Scanner(System.in); 

    //Square selection 
    if (input == 1){ 
     System.out.println("What is a length of 1 side of the Square?\n"); 
      double s1 = scan.nextDouble(); 
      double SqAns = AreaCalculator.getSquareArea(s1); 
      System.out.println("The area of you square is: " + SqAns); 

        } 

    //Rectangle selection  
     if (input == 2){ 
     System.out.println("What is the width of your rectangle?.\n"); 
      double r1 = scan.nextDouble(); 
     System.out.println("What is the height of your rectangle?\n"); 
      double r2 = scan.nextDouble(); 
      double RecAns = AreaCalculator.getRectArea(r1, r2); 
     System.out.println("The area of your rectangle is: " + RecAns);  
     } 
    //Triangle selection 
    if (input == 3){ 
     System.out.println("What is the base length of the triangle?."); 
      double t1 = scan.nextDouble(); 
     System.out.println("What is the height of your triangle?"); 
      double t2 = scan.nextDouble(); 
      double TriAns = AreaCalculator.getTriArea(t1, t2); 
     System.out.println("The area of your triangle is " + TriAns); 
    } 
    //Circle selection 
    if (input == 4){ 
     System.out.println("What is the radius of your circle?."); 
      double c1 = scan.nextDouble(); 
      double CircAns = AreaCalculator.getCircleArea(c1); 
     System.out.println("The area of your circle is " + CircAns);  

    } 
    //Exit application 
    if (input == 5){ 
     System.out.println("Goodbye."); 

    } 


} 

} 

好了,所以我在一個異常已經加入到捕獲錯誤。所以它現在在處理不使用整數的人方面有點清潔了。

號碼:什麼是廣場一側的長度?

a 爲什麼你想變得聰明?使用整數。

但是然後程序剛剛結束....我會如何讓他們回到主菜單,甚至讓他們重新輸入那裏最後的努力?

感謝,

克雷格

回答

0

至於菜單驅動程序而言,它的好,好。不過,你說: 「這工作,但後來我不得不重複的問題。」

因此,閱讀Exception handling tutorial in java

所以,你的代碼將是這個樣子:

try 
    { 
    System.out.println("Thats not a number you tool.\n"); 
    System.out.println("Now pick again\n" + 
      "1: Square?\n" + 
      "2: Rectangle?\n" + 
      "3: Triangle?\n" + 
      "4: Circle? \n" + 
      "5: Exit\n" 
      ); 


input = reader.nextDouble(); 
} 
    catch(InputMismatchException err) 
    { 
    System.out.print(err.getMessage()); 

    } 

不要忘了導入import java.util.InputMismatchExceptionimport java.util.*

並嘗試try外聲明變量塊,可能無法在外面看到。

+0

我會看看那個感謝的傢伙:) – tmcraig

+0

@tmcraig you'r welcome :) –