gpt4 book ai didi

java - 错误: cannot find symbol

转载 作者:行者123 更新时间:2023-12-02 13:40:32 30 4
gpt4 key购买 nike

感谢您花时间看到这个,我已经尝试了到目前为止我所知道的一切,但在主程序(测试类)的第 7、10、13 和 16 行仍然遇到相同的错误

public class Test
{
public static void main (String[] args)
{
int res;

res = new Calculadora.doAdd();
System.out.println("The addition is: "+ res);

res = new Calculadora.doMul ();
System.out.println("\n The multiplication is: "+ res);

res = new Calculadora.doDiv ();
System.out.println("\n The division is: "+ res);

res = new Calculadora.doSub ();
System.out.println("The substraction is: "+ res);

Calculadora obj1 = new Calculadora ();
Calculadora obj2 = new Calculadora ();
Calculadora.contar();
Calculadora.contar();
System.out.println("Obj1: "+obj1.cont + " \nObj2: "+ obj2.cont);
}
}//fin del main

第二个程序:

public class Calculadora
{
public Addition add;
public Substraction sub;
public Multiplication mul;
public Division div;

static int cont = 0;

static void contar()
{
cont++;
}

public Calculadora ()
{
add = new Addition();
sub = new Substraction();
mul = new Multiplication();
div = new Division();
}

public int doAdd ()
{
return add.doAdd ();
}

public int doSub ()
{
return sub.doSub ();
}

public int doMul ()
{
return mul.doMul ();
}

public int doDiv ()
{
return div.doDiv ();
}
}

具有操作属性的操作程序:

public class Opperation 
{
public int a, b;

public Opperation ()
{
a = 1;
b = 2;
}
}

最后,操作还是比较简单的。

public class Substraction extends Opperation //Substraction
{
public int doSub ()
{
return a-b;
}
}//substraction

public class Multiplication extends Opperation //Multiplication
{
public int doMul ()
{
return a*b;
}
}//Multiplication

public class Division extends Opperation //Division
{
public int doDiv ()
{
return a/b;
}
}//Division

public class Addition extends Opperation//Addition
{
public int doAdd ()
{
return a+b;
}
}//Addition

我知道有一种更简单的方法来制作计算器,但是老师在七个类(class)中要求它,我继续收到指向新中的点的相同错误

最佳答案

您实例化Calculadora的方式是错误的

res = new Calculadora.doAdd();  // wrong.

应该改为

res = new Calculadora().doAdd();  //correct

Calculadora cal = new Calculadora();
res = cal.doAdd();

关于java - 错误: cannot find symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42756080/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com