Java-使用扫描程序并将变量传递给其他方法

时间:2022-12-18 00:11:01

I am trying to create a text based calculator. I have a main class and a calc class. The calc class is where everything will happen, and it will be called in the main class. My problem is several variables in my calc class. It is easier to see in code.

我正在尝试创建一个基于文本的计算器。我有一个主类和一个calc类。 calc类是一切都会发生的地方,它将在主类中调用。我的问题是我的calc类中的几个变量。在代码中更容易看到。

    import java.util.Scanner;

public class Calc {
        String op;
    public void operation(String opt){
        System.out.println("What operation would you like to perform?");
        Scanner operation = new Scanner(System.in);
        op = opt;
        String op = operation.toString();
        getOp(op);
    }

    public String getOp(String op){
        return op;
    }

And later on in my code.

后来在我的代码中。

public void calculate(){
        operation(op);
        getNums(1,2);
        if(op == "Division"+"division"+"/"){
            double value = 1/2;
            System.out.println("Your answer is"+value);
        }
        if(op == "Multiplication"+"multiplication"+"*"){
            double value = 1*2;
            System.out.println("Your answer is"+value);
        }
        if(op == "Addition"+"addition"+"+"){
            double value = 1+2;
            System.out.println("Your answer is"+value);
        }
        if(op == "Subtraction"+"subtraction"+"-"){
            double value = 1/2;
            System.out.println("Your answer is"+value);
        }

    }

My problem is that I can't seem to set the value of op with the scanner, and I don't know if the value of my numbers (1 and 2) have been set either. Any help is greatly appreciated, thank you.

我的问题是我似乎无法用扫描仪设置op的值,我不知道我的数字(1和2)的值是否也已设置。非常感谢任何帮助,谢谢。

2 个解决方案

#1


1  

FWIW, I would use .nextInt instead of .toString. This would ensure it took in a number and then pass it into your store for conditional to take place.

FWIW,我会使用.nextInt而不是.toString。这将确保它接收一个数字,然后将其传递到您的商店,以便有条件地进行。

Plus, I think you would be better off using a switch statement on the calculations, in which case you could leave it as .toString or change it to .next and pass in the char or string.

另外,我认为你最好在计算中使用switch语句,在这种情况下你可以将它保留为.toString或将其更改为.next并传入char或字符串。

#2


0  

I think like βнɛƨн Ǥʋяʋиɢ's suggestion, you should not call the operation() in your Cal class. if it just prompts user and takes input, it should be in main class. As i don't see the error message so i guess one of the problem you can get is you declare your op variable one and initiate another local variable op in your operate() function to catch the user's input. Another thing is shouldn't your Scanner object call the method nextLine() instead of toString() to catch the user's input. I don't have my comp with me so can't post any code but maybe you can try to modify your code first and post some error message to be clearer.

我想像βнɛƨнǤʋяʋиɢ的建议,你不应该在你的Cal类中调用operation()。如果它只是提示用户并接受输入,它应该在主类中。因为我没有看到错误消息所以我猜你可以得到的问题之一是你声明你的op变量一个并在你的operation()函数中启动另一个局部变量op来捕获用户的输入。另一件事是你的Scanner对象不应该调用方法nextLine()而不是toString()来捕获用户的输入。我没有我的comp,所以不能发布任何代码,但也许你可以尝试修改你的代码,并发布一些错误信息,以便更清楚。

#1


1  

FWIW, I would use .nextInt instead of .toString. This would ensure it took in a number and then pass it into your store for conditional to take place.

FWIW,我会使用.nextInt而不是.toString。这将确保它接收一个数字,然后将其传递到您的商店,以便有条件地进行。

Plus, I think you would be better off using a switch statement on the calculations, in which case you could leave it as .toString or change it to .next and pass in the char or string.

另外,我认为你最好在计算中使用switch语句,在这种情况下你可以将它保留为.toString或将其更改为.next并传入char或字符串。

#2


0  

I think like βнɛƨн Ǥʋяʋиɢ's suggestion, you should not call the operation() in your Cal class. if it just prompts user and takes input, it should be in main class. As i don't see the error message so i guess one of the problem you can get is you declare your op variable one and initiate another local variable op in your operate() function to catch the user's input. Another thing is shouldn't your Scanner object call the method nextLine() instead of toString() to catch the user's input. I don't have my comp with me so can't post any code but maybe you can try to modify your code first and post some error message to be clearer.

我想像βнɛƨнǤʋяʋиɢ的建议,你不应该在你的Cal类中调用operation()。如果它只是提示用户并接受输入,它应该在主类中。因为我没有看到错误消息所以我猜你可以得到的问题之一是你声明你的op变量一个并在你的operation()函数中启动另一个局部变量op来捕获用户的输入。另一件事是你的Scanner对象不应该调用方法nextLine()而不是toString()来捕获用户的输入。我没有我的comp,所以不能发布任何代码,但也许你可以尝试修改你的代码,并发布一些错误信息,以便更清楚。