English 中文(简体)
Java-Using the scanner and adopterra to other methods
原标题:Java-Using the scanner and passing variable to other methods

我正试图建立一个基于案文的计算器。 我有一个主要班级和一个加薪班。 等级是一切都会发生的地方,并将在主要类别中进行。 我的问题是我cal的几类变量。 守则更容易见到。

    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;
    }

后来,我的法典也提到这一点。

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);
        }

    }

我的问题是,我似乎无法确定与扫描仪的同比价值,我不知道我的数字(1和2)的价值是否已经确定。 非常感谢你们。

问题回答

FWIW,我将使用next。 Int und .toString. 这将确保它用一定数目,然后将其运入贵仓库,以备进行。

此外,我认为你会更好地利用关于计算问题的开关说明,在这种情况下,你可以把它作为<条码>到String,或将其改为<条码>。

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签