我正在学习 java 并试图创建一个简单的程序来帮助我找到一种方法(如果有的话)来访问同一类的 main 方法中的非静态方法。这是我到目前为止所拥有的
import java.util.Scanner;
public class MethodVariables
{
public int num1;
public int num2;
public int add = (num1 + num2);
public int sub = (num1 - num2);
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.println("Please enter the first number: ");
String num1 = input.nextLine();
System.out.println("Please enter the second number: ");
String num2 = input.nextLine();
input.close();
// I know these wouldn't work this way but this is just to show what I am trying to accomplish
addition(add);
subtraction(sub);
}
public void addition(int add)
{
System.out.println("The sum of the two is: " +add);
}
public void subtraction(int sub)
{
System.out.println("The diference of the two is: "+sub);
}
}
如果有人知道我忽略了什么,我将不胜感激。