你知道为什么我的java计算器不工作吗? [英] Do you know why my java calculator isn't working

查看:87
本文介绍了你知道为什么我的java计算器不工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始尝试学习java,今天我尝试制作一个计算器。当我运行代码并说要添加,减去,乘,或除时,它总是出现那不是一个正确的运算符。请尝试具体...这是代码(我使用netbeans IDE)。



Calculator.java



I've recently started to try to learn java and today I tried to make a calculator. When I run the code and say to add, subtract, multiply, or divide, it always comes out with "That is not a correct operator". Please try to be specific... here is the code (I use the netbeans IDE).

Calculator.java

package calculator;
import java.util.Scanner;

public class Calculator {
    
    
    
    public static void main(String[] args) {
        
        String operator;
        
        System.out.println("Would you like to add, subtract, multiply, or divide");
        Scanner choiceOP = new Scanner(System.in);
        String choice = choiceOP.nextLine();
        
        //for a testing purpose... delete after completion
        System.out.println(choice);
        
        methods method = new methods();
        
        if (choice == "add"){
            operator = "add";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.add(num1, num2);
        } else if (choice == "subtract"){
            operator = "subtract";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.subtract(num1, num2);
        } else if (choice == "multiply"){
            operator = "multiply";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.multiply(num1, num2);
        } else if (choice == "divide"){
            operator = "divide";
            
            System.out.println("Choose your first number to " + operator);
            Scanner numScan = new Scanner(System.in);
            int num1 = numScan.nextInt();
            System.out.println("Choose your second number to " + operator);
            Scanner numScan2 = new Scanner(System.in);
            int num2 = numScan.nextInt();
            
            method.divide(num1, num2);
        } else{
            System.out.println("That is not a correct operator");
        }
        
    }
}





Methods.java





Methods.java

package calculator;

public class methods {
    
    public void add(int num1, int num2){
        double answer = num1 + num2;
        System.out.println("The answer is " + answer);
    }
    public void subtract(int num1, int num2){
        double answer = num1 - num2;
        System.out.println("The answer is " + answer);
    }
    public void multiply(int num1, int num2){
        double answer = num1 * num2;
        System.out.println("The answer is " + answer);
    }
    public void divide(int num1, int num2){
        double answer = num1 / num2;
        System.out.println("The answer is " + answer);
    }
    
}





提前致谢!!



我尝试过的事情:



我已尽力而为,包括查找它。



Thanks in advance!!

What I have tried:

I have tried everything I could including looking it up.

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较当。

当代码没有达到预期效果时,你就接近一个bug。



<$ c的值$ c> choice 未被识别为预期的运算符。

使用调试器逐行运行程序并查看 choice <的值/ code>当程序运行。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

The value of choice is not recognized as an expected operator.
Use the debugger to run the program line by line and see the value of choice as the program run.


好吧,自从我做java之后已经有一段时间了,但我很确定一些事情



a)Netbeans允许你可以单步执行代码(调试) - 相信我,这是一个很好的学习技巧,并且可以让你看到你的变量包含的内容(例如)



b )如果你的代码一直到最后,那么,你不认为检查这个吗?


well, its been some time since I did java, but Im pretty sure of a few things

a) Netbeans allows you to step through your code (debug) - trust me, its a good skill to learn, and allows you to see what your variables contain (for example)

b) if your code is all the way at the end, then, do you not think that inspecting this

Scanner choiceOP = new Scanner(System.in);
String choice = choiceOP.nextLine();

//for a testing purpose... delete after completion
System.out.println(choice);





值得 - System.out.println实际上是否在控制台上显示加或减等...如果是是的,然后



c)你可能需要在java中查看字符串比较



ie



is worthwhile - does the System.out.println actually display add or subtract etc on the console .. if it does, then

c) you might need to review string comparison in java

ie

choice == "add"

..你确定如何比较字符串?虽然它的'Legal'即编译器允许你这样做,但它实际上没有做你想要的 - 所以,有些阅读,看看 Java:==,。equals(),compareTo()和compare() [ ^ ]例如,我很确定你应该掌握这个



.. are you sure thats how to compare strings ? while its 'Legal' ie the compiler allows you to do it, its actually not doing what you want - so, some reading, look at Java: ==, .equals(), compareTo(), and compare()[^] for example, and Im pretty sure you should grasp that

if (choice.equals( "add")){

可能就是你想要的 - 只在其中一个比较中尝试它作为开始证明它

is probably what you want - try it on just one of the comparisons as a start to prove it


这篇关于你知道为什么我的java计算器不工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆