2013-10-14 12 views
-1

晚上好,我有未使用的變量和我的代碼總是拋出後確認異常,不管有效與否

我做了一個介紹性的Java類,它要求我使用開關和嘗試語句分配驗證用戶輸入。 Java應用程序的用戶應該輸入一個與數據類型相關的數字(1 - 字符串,2 - 整數,3 - 雙或4 - 退出)。該數字應該被驗證,但無論數據是否有效,驗證過程總是拋出並且異常。這樣做後,該用戶應該能夠輸入數據,並讓應用程序驗證所輸入的數據確實是選定的數據類型。鑑於我一直遇到拋出異常,我甚至無法測試這部分代碼,但肯定它不會工作,因爲我看到三個未使用的變量,根據我的文本,應該使用(strTryString ,strTryInt和strTryDouble)。在初次選擇數據類型時,我不能在思想上做出連接,然後程序會說「用戶選擇雙精度,比較輸入[strTryDouble?]到雙精度數據類型的參數」。任何幫助,這將不勝感激。以下是我迄今:

import java.io.*; 
import javax.swing.JOptionPane; 

public class MyType 
{ 
    public static void main(String[] args) 
    { 
     // declare class variables 
     String strChoice, strTryString, strTryInt, StrTryDouble; 
     int choice, tryInt; 
     double tryDouble; 
     boolean done = false; 

     // loop while not done 
     while(!done) 
     { 
      try 
      { 
       strChoice = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program"); 

       choice = Integer.parseInt(JOptionPane.showInputDialog(null,strChoice)); 

       switch(choice) 
       { 
        case 1: 
         JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String."); 
         break; 

        case 2: 
         tryInt = Integer.parseInt(strTryInt); 
         JOptionPane.showMessageDialog(null, "Correct."); 
         break; 

        case 3: 
         tryDouble = Double.parseDouble(strTryDouble); 
         JOptionPane.showMessageDialog(null, "Correct."); 
         break; 

        case 4: 
         done = true; 
         JOptionPane.showMessageDialog(null, "The program will now close."); 
         break; 

        default: 
         throw new NumberFormatException(); 
       } 
      } 
      catch(NumberFormatException e) 
      { 
       JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.","Error",JOptionPane.INFORMATION_MESSAGE); 
      } 
     } 
    } 
} 

回答

1
  • strTryDouble被大寫(StrTryDouble)在聲明。
  • 輸入和解析有點不清楚。

我下面的作品(JDK 6;改線標記/***/):

import java.io.*; 
import javax.swing.JOptionPane; 

public class MyType 
{ 
    public static void main(String[] args) 
    { 
     // declare class variables 
     String strChoice, strTryString, strTryInt, strTryDouble; /***/ 
     int choice, tryInt; 
     double tryDouble; 
     boolean done = false; 
     boolean ok; /***/ 

     // loop while not done 
     while(!done) 
     { 
      try 
      { 
       strChoice = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program"); 

       choice = Integer.parseInt(strChoice); /***/ 

       switch(choice) 
       { 
        case 1: 
         JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String."); 
         break; 

        case 2: 
         strTryInt = JOptionPane.showInputDialog(null,"Enter an integer"); /***/ 
         ok = true; 
         try { /***/ 
          tryInt = Integer.parseInt(strTryInt); 
         } catch(NumberFormatException e) { /***/ 
          ok = false;  /***/ 
         } 
         JOptionPane.showMessageDialog(null, ok?"Correct.":"Not an integer"); /***/ 
         break; 

        case 3: 
         strTryDouble = JOptionPane.showInputDialog(null,"Enter a double"); /***/ 
         tryDouble = Double.parseDouble(strTryDouble); 
         JOptionPane.showMessageDialog(null, "Correct."); 
         break; 

        case 4: 
         done = true; 
         JOptionPane.showMessageDialog(null, "The program will now close."); 
         break; 

        default: 
         throw new NumberFormatException(); 
       } 
      } 
      catch(NumberFormatException e) 
      { 
       JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.","Error",JOptionPane.INFORMATION_MESSAGE); 
      } 
     } 
    } 
} 

要獲得類型的選擇,JOptionPane.showInputDialog從用戶返回字符串,然後Integer.parseInt分析它。

要獲取該值,請使用相同的模式:獲取一個字符串,然後解析它。獲取每個case中的字符串會處理未初始化的變量。

在情況2中,我添加了一個如何檢查它是否實際上是整數然後繼續執行代碼的示例。一般來說,您可以捕捉異常並設置一個標誌(ok)進行處理。只有當函數報告正常情況(例如,不良用戶輸入)時,我纔會使用它。對於真正的錯誤,我讓異常傳播到更高層次的catch塊。

1

這裏有一個更加高效和完善的解決問題的方法:

import java.io.*; 
import javax.swing.JOptionPane; 

public class MyType 
{ 
public static void main(String[] args) 
{ 
    // declare class variables 
    String strChoice=""; // you didn't need the other variables 
    Integer choice, tryInt; 
    double tryDouble; 
    boolean done = false; 

    // loop while not done 
    while(!done) 
    { 
     try 
     { 
      strChoice = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program"); 

      if(strChoice.equals("")) // refuses blank answers, must enter a choice 
      { 
       JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.","Error",JOptionPane.INFORMATION_MESSAGE); 
       continue; 
      } 

      choice = Integer.parseInt(strChoice); 
      if(choice !=4) // so the input dialog doesn't appear when choice is 4 
      strChoice = JOptionPane.showInputDialog(null,strChoice); 




      switch(choice) 
      { 
       case 1: 
        JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String."); 
        break; 

       case 2: 
        tryInt = Integer.parseInt(strChoice); 
        JOptionPane.showMessageDialog(null, "Correct."); 
        break; 

       case 3: 
        tryDouble = Double.parseDouble(strChoice); 
        JOptionPane.showMessageDialog(null, "Correct."); 
        break; 

       case 4: 
        done = true; 
        JOptionPane.showMessageDialog(null, "The program will now close."); 
        break; 

       default: 
       { if((choice > 4) || (choice == null)) // must be 1 - 4, & not blank 
        JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.","Error",JOptionPane.INFORMATION_MESSAGE); 

       } 
      } 
     } 
     catch(NumberFormatException e) // this should ONLY occur when type is wrong 
     { 
      JOptionPane.showMessageDialog(null,"Your input can't be stored into that type.","Error",JOptionPane.INFORMATION_MESSAGE); 
     } 
    } 
} 
} 

此外,在情況下,你應該分析strChoice,不choicechoice之前已經轉換爲int,所以它不會是一個很好的比較。使用strString真正比較用戶輸入和類型。

我也改變intIntegerint是基本數據類型,並且不能檢查null值。 Integer可以做所有int可以,但它是一個對象,並且對象可以爲null。如果選擇恰好是null,我們可以檢查並做一些事情。

雖然您接受的答案沒有錯,但代碼太重複(不是一個好的編程習慣),並且與您的原始代碼相差甚遠。當「錯誤」信息不符合真正錯誤的描述時,也有例外。另外,程序變得更加複雜:編程越簡單越好。

希望這有助於! 如果您有任何問題,請告訴我!

乾杯! :)

JLL

相關問題