简体   繁体   English

如何使用Scanner / System.in中尚未设置的字符串的值-Java

[英]How to use the value of a string that is yet to be set by scanner/system.in - Java

I am trying to create a calculator. 我正在尝试创建一个计算器。 It may not be the most efficient way but I would like help on what I have done so far. 这可能不是最有效的方法,但是我想对到目前为止的工作有所帮助。

    package me.Nelsin.Calculator;


    import java.text.DecimalFormat;
    import java.util.Scanner;


public class Calculator {


public static void main(String args[]) {

    double answer;
    //2 numbers
    System.out.println("Enter your first number: ");
    Scanner fnumb = new Scanner(System.in);
    double fnum = fnumb.nextDouble();

    System.out.println("Enter your operation, *, /, +, -: ");
    Scanner operation = new Scanner(System.in);
    String op = operation.nextLine();

    System.out.println("Enter your second number: ");
    Scanner snumb = new Scanner(System.in);
    double snum = snumb.nextDouble();

    //Answers
    if(op.equals("*")) {
        answer = fnum * snum;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println(df.format(answer));
    }   
    if (op.equals("/")) {
        answer = fnum / snum;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println(df.format(answer));
    }
    if (op.equals("+")) {
        answer = fnum + snum;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println(df.format(answer));
    } 
    if (op.equals("-")) {
        answer = fnum - snum;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println(df.format(answer));  
        }
    System.out.println("Enter your operation, *, /, +, -: ");
    Scanner operation2 = new Scanner(System.in);
    String op2 = operation2.nextLine();

    System.out.println("Enter your third number here: ");
    Scanner tnumb = new Scanner(System.in);
    double tnum = tnumb.nextDouble();

    if (op2.equals("*")) {
    double answer2 = answer * tnum;
    DecimalFormat df = new DecimalFormat("#.##");
    System.out.println(answer2);
         }
    }
}

So my error is here: 所以我的错误在这里:

    double answer2 = answer * tnum;

I believe its because answer is not set until the user runs the program. 我相信这是因为直到用户运行该程序才设置答案。 The error: 错误:

The local variable may have not been initialised.

The reason for this error is because you set variable "answer" within an if statements. 发生此错误的原因是因为您在if语句中设置了变量“ answer”。 to fix the error add else statement after your if condition like 修复错误,在条件如if后面添加else语句

... ...

else{
answer=0.0;
}

or a easier solution would be to say: 或更简单的解决方案是说:

double answer=0;

You are getting the error because the local variable is not initialized and local variables doesn't have a default values . 因为尚未初始化局部变量局部变量没有默认值,您会得到错误。 Assign value to the answer variable when you declare it. 声明变量时将其赋值。

double answer = 0.0;

Instance variables are initialized to null or to their default primitive values, if they are primitives. Instance variables被初始化为null或为其默认的原始值(如果它们是原始值)。

Local variables are undefined and are not initialized and so it is your responsibility for setting the initial value. Local variables未定义且未初始化,因此您有责任设置初始值。

so change this double answer; 所以改变这个double answer; to double answer = 0; double answer = 0;

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

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