简体   繁体   中英

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. to fix the error add else statement after your if condition like

...

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.

Local variables are undefined and are not initialized and so it is your responsibility for setting the initial value.

so change this double answer; to double answer = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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