编译时,表示变量secondNumberString可能尚未初始化。但我已将其声明为字符串并在secondNumber开关中初始化它。我在这做错了什么?我只想将secondNumber(比如输入5)转换为一个名为seondNumberString的字符串并将其作为五个然后显示它。必须使用开关。
/*
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 :(得分:4)
但我已将其声明为字符串并在secondNumber开关中初始化它。
好吧,如果你遇到任何指定的情况,你就有了。但是你的default
案例只是:
default:
JOptionPane.showMessageDialog(null, "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE);
您认为secondNumberString
之后的价值是什么?您应该在那里指定一个值 - 或者在那个点退出。鉴于您希望在切换之前验证secondNumber
,我会抛出某种RuntimeException
而不是显示消息对话框。编译器会知道在那时你不会继续尝试使用变量,所以将在switch语句之后明确地分配。
请注意,即使我们可以告诉您在switch语句中实际上永远不会遇到default
个案,definite assignment和reachability的规则在Java中没有涵盖编译器应该推断secondNumber
必须是0..9之一的想法。我们需要告诉它我们真的,真的不希望到达这里 - 这就是为什么异常是最好的选择。
顺便说一句,如果您将一个巨大的方法分解为许多不同的方法,那么您的代码将更加清晰 。除此之外,您不需要那么多局部变量。我还建议仅在首次使用时声明变量,而不是在顶部声明所有内容。你还应该学会使用数组 - 这将完全摆脱switch语句......
答案 1 :(得分: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不会获得值。
您可以在默认部分设置一个值,以便删除此警告。
你的逻辑似乎必须介于0到8之间,所以我认为你可以设置为默认
secondNumberString = null or secondNumberString = ""
答案 2 :(得分:0)
Java严格要求变量沿着代码的每条可能路径进行初始化,直到使用变量的位置。
在上文中,secondNumberString
语句的default
案例中未初始化switch
。
答案 3 :(得分:-1)
编译器不够聪明,通过secondNumber
循环(确保while
来确定您已涵盖secondNumber
的所有可能值。必须在0到9之间)。
您需要在大secondNumberString
之前为null
提供""
或switch/case
之类的默认值,以防止发生这种情况,或者default
情况下。