如何从一种方法获取变量到另一种方法?如何从假设进入void add()得到int x?

时间:2017-11-27 02:07:17

标签: java

我想将x和y的值传递给方法add() 公共类算术{

public static void assuming(){ 

System.out.println("Please insert the first number");
Scanner read = new Scanner(System.in);
int x = read.nextInt();

System.out.println("Please insert the second number");
Scanner read1 = new Scanner(System.in);
int y = read1.nextInt();
}


void add()
{
// How to get values of int x and int y from upper method in here ?
}

1 个答案:

答案 0 :(得分:1)

在假设方法中创建一个methdd调用add(x,y);

int answer = add(x,y);

之后修改您的add功能

int add(int x,int y)
{
   return x+y;
}