简体   繁体   English

我怎样才能让我的价值观不存在if陈述中?

[英]How can I keep my value from an if-statement?

I tried making a very basic calculator, but if I try to output the calculated result I get the error that my variable "result" has not been initialised, even though I did initialise it inside the if-statement at the bottom. 我尝试制作一个非常基本的计算器,但是,如果我尝试输出计算结果,则会收到错误消息,即使我在底部的if语句中将变量“结果”初始化了,但尚未初始化。 When I put the "System.out" line in my if-statement it works, so I have been wondering how can initialise a variable in my statement and keep the value. 当我在if语句中放入“ System.out”行时,它起作用了,所以我一直想知道如何在我的语句中初始化变量并保留值。

But all I found were threads that talked about using "return" to give out a value, but not a variable. 但是我发现的所有线程都在谈论使用“返回”给出一个值,而不是一个变量。

import javax.swing.JOptionPane;
class Calculator{
public static void main(String[] args)
{
    double z1, z2, result;
    String input, s;

    input = JOptionPane.showInputDialog("Input a number:");

  //check whether or not the input is a number
        if(isNumber(input) == false)
        {
            JOptionPane.showMessageDialog(null, "Input a real Number", "Error",JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }

    z1 = Double.parseDouble(input);

    input = JOptionPane.showInputDialog("Input a second number:");

  //check whether or not the input is a number
        if(isNumber(input) == false)
        {
            JOptionPane.showMessageDialog(null, "Input a real number", "Error",JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    z2 = Double.parseDouble(input);

    s = JOptionPane.showInputDialog("Input an operation(+,-,*,/):");        

    if(s.equals("+"))
    { result = z1 + z2; }
    if(s.equals("-"))
    { result = z1 - z2; }
    if(s.equals("*"))
    { result = z1 * z2; }
    if(s.equals("/"))
    { result = z1 / z2; }

    System.out.println(result);

Since this code: 由于此代码:

if(s.equals("+"))
{ result = z1 + z2; }
if(s.equals("-"))
{ result = z1 - z2; }
if(s.equals("*"))
{ result = z1 * z2; }
if(s.equals("/"))
{ result = z1 / z2; }

does not have a final else statement it is not guaranteed that result will be initialized. 没有最终的else语句,因此不能保证result将被初始化。
One way to overcome this problem: 解决此问题的一种方法:

    if (s.equals("+")) {
        result = z1 + z2;
    } else if (s.equals("-")) {
        result = z1 - z2;
    } else if (s.equals("*")) {
        result = z1 * z2;
    } else if (s.equals("/")) {
        result = z1 / z2;
    } else {
        result = 0.0;
    }

or in the definition: 或在定义中:

double result = 0.0;

The problem is that the environment checks whether everything is okay and sees that you have a few ifs where the variable might be initialized and finds that result is not guaranteed to be initialized. 问题是,环境检查是否一切正常,并认为你有几个IFS其中变量可能会被初始化,并认为result不能保证被初始化。 As a result you get this error. 结果,您得到此错误。 Try to initialize it at the start: 尝试在开始时对其进行初始化:

double z1, z2, result = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM