简体   繁体   中英

I tried to write a basic calculator but java doesn't see my number, can't find what's wrong :(

    // constants
    final String LINE = "----------------";
    final String VALUE = " +,-,*,/ value";
    final String CLEAR = "Clear";
    final String QUIT = "Quit ";
    final int ZERO = 0;

    // variables
    double first;
    String function;
    double number;

    // program code
    System.out.println( "Start...");
    System.out.println( "Welcome to \"SimpleCalc\" ... ");
    first = 0;
    // 1.Calculations
    do 
    {
      System.out.println(LINE);
      System.out.println( "[" + first + "]" );
      System.out.println(VALUE);
      System.out.println(CLEAR);
      System.out.println(QUIT);
      System.out.println(LINE);
      System.out.println(" SELECT :");
      function = scan.next();
      if (function.equals("+") || function.equals("-") || function.equals("*") || function.equals("/"))
      {
        number = scan.nextDouble();
        if ( function.equals("+") )
        {
          first = first + number;
        }
        else if (function.equals("-") )
        {
          first = first - number;
        }
        else if (function.equals("/") )
        {
          first = first / number;
        }
        else if (function.equals("*") )
        {
          first = first * number;
        }

      }
      else if (function.equals("Clear") );
      {
        first = ZERO;
      }

    }
    while ( function != "q" );
    //2. Exit
    // todo...

    System.out.println( "End.");
}

This is my code, I want to get Welcome to "SimpleCalc"...


[ 0.0 ]

+,-,*,/ value Clear

Quit

Select: + 25.0


[ 25.0 ]

+,-,*,/ value Clear

Quit

Select: / 4


[ 6.25 ]

+,-,*,/ value Clear

Quit

Select: Clear


[ 0.0 ]

+,-,*,/ value Clear

Quit

Select: q

an output like this. But something wrong and I can't find what's wrong. And I get my output like this;

Welcome to "SimpleCalc"...


[ 0.0 ]

+,-,*,/ value Clear

Quit

Select: + 25.0


[ 0.0 ]

+,-,*,/ value Clear

Quit

Select:


Thanks for help.

Here you go.

import java.util.Scanner;

public class Calculator {

    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        // constants
        final String LINE = "----------------";
        final String VALUE = " +,-,*,/ value";
        final String CLEAR = "Clear";
        final String QUIT = "Quit";
        final int ZERO = 0;

        // variables
        double result;
        String function;
        double number;

        // program code
        System.out.println("Start...");
        System.out.println("Welcome to \"SimpleCalc\" ... ");
        result = 0;
        // 1.Calculations
        while (true) {
            System.out.println(LINE);
            System.out.println("[" + result + "]");
            System.out.println(VALUE);
            System.out.println(CLEAR);
            System.out.println(QUIT);
            System.out.println(LINE);
            System.out.println(" SELECT :");
            function = scan.next();
            if (function.equalsIgnoreCase("q")) {
                break;
            }
            if (function.equalsIgnoreCase("Clear")) {
                result = ZERO;
            } else {
                number = scan.nextDouble();
                switch (function) {
                    case "+":
                        result = result + number;
                        break;
                    case "-":
                        result = result - number;
                        break;
                    case "/":
                        result = result / number;
                        break;
                    case "*":
                        result = result * number;
                        break;
                }
            }
        }
        //2. Exit
        // todo...

        System.out.println("End.");
    }
}

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