简体   繁体   English

我试图编写一个基本的计算器,但 java 看不到我的号码,找不到问题:(

[英]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"...这是我的代码,我想欢迎使用“SimpleCalc”...


[ 0.0 ] [ 0.0 ]

+,-,*,/ value Clear +,-,*,/ 值 清除

Quit退出

Select: + 25.0选择:+ 25.0


[ 25.0 ] [ 25.0 ]

+,-,*,/ value Clear +,-,*,/ 值 清除

Quit退出

Select: / 4选择:/4


[ 6.25 ] [ 6.25 ]

+,-,*,/ value Clear +,-,*,/ 值 清除

Quit退出

Select: Clear选择:清除


[ 0.0 ] [ 0.0 ]

+,-,*,/ value Clear +,-,*,/ 值 清除

Quit退出

Select: q选择: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 ] [ 0.0 ]

+,-,*,/ value Clear +,-,*,/ 值 清除

Quit退出

Select: + 25.0选择:+ 25.0


[ 0.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.");
    }
}

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

相关问题 Java Udemy 课程闰年计算器 - 无法弄清楚我做错了什么 - Java Udemy course leap year calculator - can't figure out what am I doing wrong 我找不到我的 sql 语法有什么问题 - I can't find out what's wrong with my sql syntax 解析错误; 我已经尝试过,无法弄清楚出了什么问题? - Parsing error; I have tried and can't figure out what's wrong? 我在 Java 中的方法找不到变量的值并将其设置为 0。我做错了什么? - My method in Java can't find the value of the variable and sets it as 0. What am I doing wrong? Scene Builder预览不适用于我的Java程序(基本计算器) - Scene Builder preview doesn't apply to my Java program (basic calculator) 我的Java代码出现错误,但看不到它有什么问题。 救命? - I'm getting an error in my Java code but I can't see whats wrong with it. Help? 数组索引是“越界”。 但我看不出有什么问题 - Array Index is “Out of Bounds”. But I can't see what's wrong 怎么了 我收到java.lang.ClassCastException错误,但看不到哪里出错了 - What is wrong here? I get a java.lang.ClassCastException error but I can't see where I have gone wrong 我自己编写的Calculator.java有什么问题? - What's wrong with my self-written Calculator.java? instanceof似乎不起作用-我的代码有什么问题? - instanceof doesn't seem to work - what's wrong with my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM