2014-10-08 197 views
1

編譯它時說變量secondNumberString可能沒有被初始化。但是我已經將它聲明爲一個字符串並在secondNumber開關中初始化它。我在這裏做錯了什麼?我只是想將secondNumber轉換爲一個名爲seondNumberString的字符串,並將其輸入爲5,然後顯示它。它必須使用開關。變量初始化並聲明後可能尚未初始化

/* 
Programmer: 
Date: Wednesday, October 8, 2014 
Description: A simple calculator 
*/ 

import javax.swing.JOptionPane;  // Imports the JOptionPane class 

public class CalculatorStevenHasaka 
{ 
    public static void main(String[] args) 
    { 
     String input;     // To temporarily hold the input 
     String firstNumberString;  // To hold the string name of the first number 
     String secondNumberString; // To hold the string name of the second number 
     String operatorString;  // To hold the string name of the operator 
     int firstNumber;    // To hold the first number 
     int secondNumber;    // To hold the second number 
     int answer;     // To hold the answer 
     char operator;    // To hold the operator 

     // Ask the user for a number from 0-9 (The first number) 
     input = JOptionPane.showInputDialog(null, "Please enter the first number. \nIt must be from 0 to 9. \nNo decimals, please.", "Calculator v1.0", JOptionPane.QUESTION_MESSAGE); 
     // Convert the input to an integer 
     firstNumber = Integer.parseInt(input); 


     // Validate the input of firstNumber 
     while (firstNumber < 0 || firstNumber > 9) 
     { 
     // Ask the user for a number from 0-9 
     input = JOptionPane.showInputDialog(null, "Invalid number! \nIt must be a number from 0 to 9. \nNo decimals, please.", "Invalid Number", JOptionPane.WARNING_MESSAGE); 
     // Convert the number to an integer 
     firstNumber = Integer.parseInt(input); 
     } // End of firstNumber validation 


     // Ask the user for an operator 
     input = JOptionPane.showInputDialog(null, "Please input an operator. \nYou can use +, -, *, /, or ^", "Calculator v1.0", JOptionPane.QUESTION_MESSAGE); 
     // Convert the input to a character 
     operator = input.charAt(0); 


     // Validate the input of the operator 
     while ((operator != '+') && (operator != '-') && (operator != '*') && (operator != '/') && (operator != '^')) 
     { 
     // Ask the user for an operator 
     input = JOptionPane.showInputDialog(null, "Invalid operator! \nYou can only use +, -, *, /, or ^", "Invalid Operator", JOptionPane.WARNING_MESSAGE); 
     // Convert the input to a character 
     operator = input.charAt(0); 
     } // End of operator validation 


     // Ask the user for a number from 0-9 (The second number) 
     input = JOptionPane.showInputDialog(null, "Please enter the second number. \nIt must be from 0 to 9. \nNo decimals, please.", "Calculator v1.0", JOptionPane.QUESTION_MESSAGE); 
     // Convert the number to an integer 
     secondNumber = Integer.parseInt(input); 


     // Validate the input of secondNumber 
     while (secondNumber < 0 || secondNumber > 9) 
     { 
     //Ask the user for a number from 0-9 
     input = JOptionPane.showInputDialog(null, "Invalid number! \nIt must be a number from 0 to 9. \nNo decimals, please.", "Invalid Number", JOptionPane.WARNING_MESSAGE); 
     // Convert the number to an integer 
     secondNumber = Integer.parseInt(input); 
     } // End of secondNumber validation 


     // Convert firstNumber to a string 
     switch (firstNumber) 
     { 
     case 0: 
      firstNumberString = "Zero"; 
      break; 
     case 1: 
      firstNumberString = "One"; 
      break; 
     case 2: 
      firstNumberString = "Two"; 
      break; 
     case 3: 
      firstNumberString = "Three"; 
      break; 
     case 4: 
      firstNumberString = "Four"; 
      break; 
     case 5: 
      firstNumberString = "Five"; 
      break; 
     case 6: 
      firstNumberString = "Six"; 
      break; 
     case 7: 
      firstNumberString = "Seven"; 
      break; 
     case 8: 
      firstNumberString = "Eight"; 
      break; 
     case 9: 
      firstNumberString = "Nine"; 
      break; 
     default: 
      JOptionPane.showMessageDialog(null, "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE); 
     } // End of firstNumber switch 


     // Convert secondNumber to a string 
     switch (secondNumber) 
     { 
     case 0: 
      secondNumberString = "zero"; 
      break; 
     case 1: 
      secondNumberString = "one"; 
      break; 
     case 2: 
      secondNumberString = "two"; 
      break; 
     case 3: 
      secondNumberString = "three"; 
      break; 
     case 4: 
      secondNumberString = "four"; 
      break; 
     case 5: 
      secondNumberString = "five"; 
      break; 
     case 6: 
      secondNumberString = "six"; 
      break; 
     case 7: 
      secondNumberString = "seven"; 
      break; 
     case 8: 
      secondNumberString = "eight"; 
      break; 
     case 9: 
      secondNumberString = "nine"; 
      break; 
     default: 
      JOptionPane.showMessageDialog(null, "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE); 
     } // End of secondNumber switch 


     // Convert operator to a string and perform the calculations 
     if (operator == '+') 
     { 
     operatorString = "plus"; 
     answer = firstNumber + secondNumber; 
     JOptionPane.showMessageDialog(null, "Blah: " + secondNumberString); 
     } 
     else if (operator == '-') 
     { 
     operatorString = "minus"; 
     } 
     else if (operator == '*') 
     { 
     operatorString = "multiplied by"; 
     } 
     else if (operator == '/') 
     { 
     operatorString = "divided by"; 
     } 
     else if (operator == '^') 
     { 
     operatorString = "to the power of"; 
     } 
     else 
     { 
     JOptionPane.showMessageDialog(null, "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE); 
     } // End of operator if/else if/else      
    } // End of main 
} // End of public class        
+0

'secondNumberString'未在'default'情況下初始化。 – 2014-10-08 21:12:07

回答

-1

編譯器是不是足夠聰明,制定出你所投保的secondNumber每一個可能的值,你while循環的方式(這確保了secondNumber必須在0和9之間)。

你需要給secondNumberString默認值大switch/case之前如null""防止這種情況發生,或者在default情況。

4

但我已經聲明它爲一個字符串並在secondNumber開關中初始化它。

那麼,你有,如果你有任何指定的情況下。但是,你的default情況就是:

default: 
    JOptionPane.showMessageDialog(null, "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE); 

你有什麼期望的secondNumberString值是之後呢?你應該在那裏分配一個值 - 或者在那個點退出。鑑於您希望在切換之前驗證secondNumber,我會拋出某種RuntimeException而不是顯示消息對話框。編譯器會知道在那時你不會繼續嘗試使用該變量,因此在switch語句後肯定會分配

注意,即使我們可以告訴你永遠不會真正打在switch語句中default情況下,definite assignment和Java中reachability規則沒有覆蓋的想法,編譯器應該有理由相信secondNumber有成爲0..9之一。我們需要告訴它,我們真的,真的不希望到達這裏 - 這就是爲什麼例外是這裏最好的選擇。

順便說一句,如果你想將你的一個巨大的方法分解成許多不同的方法,那麼你的代碼將會是更清晰的。除了其他任何東西,你都不需要那麼多的局部變量。我還建議只在首次使用時聲明變量,而不是在頂部聲明所有變量。你也應該學會使用陣列 - 這將擺脫完全的switch語句...

+0

@wrongAnswer:你爲什麼問我?你認爲我是低調的嗎?我沒有...但我會評論你的答案。 – 2014-10-08 21:13:43

+0

感謝您的解釋! – StackFlowed 2014-10-08 21:15:19

+0

@Jon,你的回答並不完全錯誤,但稍微有點不對。由於'while'循環在上面,'default'事件實際上不會被打到。但編譯器沒有考慮到這一點。 – 2014-10-08 21:16:17

0

編譯器看到的問題是在這裏:

// Convert secondNumber to a string 
    switch (secondNumber) 
    { 
    case 0: 
     secondNumberString = "zero"; 
     break; 
    case 1: 
     secondNumberString = "one"; 
     break; 
    case 2: 
     secondNumberString = "two"; 
     break; 
    case 3: 
     secondNumberString = "three"; 
     break; 
    case 4: 
     secondNumberString = "four"; 
     break; 
    case 5: 
     secondNumberString = "five"; 
     break; 
    case 6: 
     secondNumberString = "six"; 
     break; 
    case 7: 
     secondNumberString = "seven"; 
     break; 
    case 8: 
     secondNumberString = "eight"; 
     break; 
    case 9: 
     secondNumberString = "nine"; 
     break; 
    default: 
     JOptionPane.showMessageDialog(null, "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE); 
    } // End of secondNumber switch 

secondNumber可能不是0之間8,因此造成secondNumberString不會得到一個值。

您可以在默認部分設置一個值以刪除此警告。

你的邏輯似乎是,它必須是8之間0只,所以我認爲你可以設定在默認

secondNumberString = null or secondNumberString = "" 
+0

我設置我的數字字符串爲空,它刪除了惱人的錯誤消息。謝謝你,先生。 – StanRadner 2014-10-08 21:18:30

+0

不是我特別興奮的解決方案。 – Andreas 2014-10-08 21:23:56

0

Java的嚴格要求變量沿着任何可能的路徑通過代碼進行初始化,下至指出使用變量的地方。

在上文中,secondNumberString未在default情況下switch語句的初始化。

+0

還有比這更多的東西。他有while循環,只有'secondNumber'介於0和9之間時才能退出;所以他實際上已經涵蓋了每個案例。不幸的是,編譯器不夠聰明來琢磨這一點。 – 2014-10-08 21:15:40

+1

@DavidWallace - 這不是編譯器(或JDK驗證程序)的工作方式。該值必須沿EVERY路徑設置,而不考慮控制流量的值。這寫入J​​ava虛擬機規範。如果編譯器「足夠聰明」接受它,JVM仍然會拒絕它。 – 2014-10-08 21:18:35