Tit*_*anC 1 java if-statement joptionpane
我一直得到一个错误变量F3可能尚未在您看到的最后一行代码中初始化.
我究竟做错了什么?
{
Float F1,F2, F3;
F1 = Float.parseFloat(
JOptionPane.showInputDialog("Enter a number and press ok."));
F2 = Float.parseFloat(
JOptionPane.showInputDialog("Enter a second number and press ok."));
if(F1 >= F2)
{
F3=(F1 * F2) + (F1 * 2);
}
if(F2 >= F1)
{
F3 =(F1 + F2) + (F2 * 5);
}
DecimalFormat dec = new DecimalFormat("##.##");
JOptionPane.showMessageDialog(null,"Your calculations are:" +(F3),"Caculations", JOptionPane.INFORMATION_MESSAGE);
Run Code Online (Sandbox Code Playgroud)
您应该使用if/else而不是if/if here,以便编译器知道F3将始终设置为值.
以下代码等同于您的if/if语句:
if(F1 > F2) //The = here will always be overridden by the other if clause in your original statement so it's redundant.
{
F3=(F1 * F2) + (F1 * 2);
}
else
{
F3 =(F1 + F2) + (F2 * 5);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2556 次 |
| 最近记录: |