如何在Java中对来自另一个类的变量使用运算符

huangapple go评论72阅读模式
英文:

How can you use operators on a variable from another class in java

问题

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Calculator calculator = new Calculator();
    int num1, num2;
    int result;
    Operator operator = new Operator();

    System.out.println("What operation do you want to perform?");
    String chosenOperator = input.nextLine();

    System.out.print("Enter the first number: ");
    num1 = input.nextInt();

    System.out.print("Enter the second number: ");
    num2 = input.nextInt();

    if (chosenOperator.equals("Subtract") || chosenOperator.equals("-"))
        result = operator.subtract(num1, num2);
    else if (chosenOperator.equals("Add") || chosenOperator.equals("+"))
        result = operator.add(num1, num2);
    else if (chosenOperator.equals("Multiply") || chosenOperator.equals("*"))
        result = operator.multiply(num1, num2);
    else
        result = 0; // Default value for unsupported operations

    System.out.println("Result: " + result);
}

---

public class Calculator {
    public int subtract(int num1, int num2) {
        return num1 - num2;
    }

    public int add(int num1, int num2) {
        return num1 + num2;
    }

    public int multiply(int num1, int num2) {
        return num1 * num2;
    }
}

Please note that your initial code had a few issues, such as mixing classes and methods with inappropriate names, and trying to subtract, add, or multiply instances of a class (Main) instead of numbers. The revised code above provides a corrected version to achieve the calculator functionality you described.

英文:

I'm trying to make a calculator in which you have to first say what kind of operator you want to use, and then add 2 numbers to calculate it. To do this, I use 2 classes 1 for the inputs and calculations and the other one for the operators. In the second class is the error for this answer = int1 - int2;. The error I get is that I can't use the operators on com.company.Main. Can anyone tell me how to fix the error?

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
mathe decrease = new mathe();
int int1, int2;
mathe answer = new mathe();
mathe operator= new mathe ();
System.out.println( "What do you want to do?");
String chosenOperator = input.nextLine();
System.out.print("Add a number: ");
int1 = input.nextInt();
System.out.print("Add another number: ");
int2 = input.nextInt();
if (chosenOperator.equals("Decrease") || chosenOperator.equals("-"))
operator.decrease(answer.toString());
else if (chosenOperator.equals("Adding") || chosenOperator.equals("+"))
operator.adaption(answer.toString());
else if (chosenOperator.equals("Times") || chosenOperator.equals("*"))
operator.times(answer.toString());
}

public class mathe {
Main int1 = new Main();
Main int2 = new Main();
public void decrease(String answer){
answer = int1 - int2;
System.out.println(answer);
}
public void adaption(String answer){
answer = int1 + int2;
System.out.println(answer);
}
public void times(String answer){
answer = int1 * int2;
System.out.println(answer);
}
}

答案1

得分: 2

因为int1和int2实际上不是int值,而是指向Main类的对象引用,原因就在于此。如果你想从主类中访问两个int,可以像这样做:

public class Main 
{
    int a; 
    int b; 

    public Main(int a, int b) 
    { 
        this.a = a; 
        this.b = b; 
    }

    //创建getter和setter方法来初始化和获取这些值 
}

然后在另一个类中,你可以这样调用它们:

Main m = new Main(1, 2);
public void decrease(String answer){
    answer = m.getIntOneValue() - m.getIntTwoValue();
    System.out.println(answer);
}

现在你可以在任何想要的类中访问这些值,只要你正确地访问它们的值。更多示例,请参阅这个链接

英文:

The reason is because int1 and int2 aren't actually int values, instead they are object references to the class Main. What you would do, if you're trying to access two ints from the main class you could do something like this:

public class Main 
{
int a; 
int b; 
public Main(int a, int b) 
{ 
this.a = a; 
this.b = b; 
}
//create getters and setters to initialize and get the values 
}

Then in the seperate class, you can call them like this:

Main m = new Main(1, 2);
public void decrease(String answer){
answer = m.getIntOneValue() - m.getIntTwoValue();
System.out.println(answer);
}

Now you can access the values across any class you want, as long as you correctly access their values. Refer to this link for more examples:

huangapple
  • 本文由 发表于 2020年8月30日 06:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63652261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定