-3
    // 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.

  • 4
    `while (function != "q")` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Nov 06 '15 at 16:32
  • 1
    but what is strange you seems to know that based on `if (function.equals("Clear") )` – Pshemo Nov 06 '15 at 16:33
  • 1
    Oh and don't add `;` right after `if(condition)` like you do in `else if (function.equals("Clear") );` – Pshemo Nov 06 '15 at 16:34
  • 1
    In your post it is totally unclear, which is input and what is output. Try to break it down to one simple example, to demonstrate what does not work. – hotzst Nov 06 '15 at 16:35
  • @Pshemo You should post all your comments as answer :) – sam Nov 06 '15 at 16:36
  • 2
    @sam Nah, `==` vs `equals` is duplicate (but I already used my close vote so can't change it now), and `;` after `if` is also duplicate or simply typographical problem which is off-topic on Stack Overflow. I don't want to prevent automatic deletion of low quality question like this one by posting answer which could potentially get upvote/acceptence mark. I may help OP by posting comments but that is all. – Pshemo Nov 06 '15 at 16:38
  • @Pshemo TBH I didn't knew about automatic deletion – sam Nov 06 '15 at 16:43
  • @sam More info about automatic deletion here: http://meta.stackexchange.com/questions/78048/enable-automatic-deletion-of-old-unanswered-zero-score-questions-after-a-year/92006#92006 – Pshemo Nov 06 '15 at 19:31

1 Answers1

1

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.");
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Doc
  • 10,831
  • 3
  • 39
  • 63