編譯它時說變量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
'secondNumberString'未在'default'情況下初始化。 – 2014-10-08 21:12:07