scanner类作为函数参数-如何引用它

kninwzqo  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(256)

有人能告诉我如何将scanner类作为函数参数引用吗?我的意思是:如果我有一个主要功能:

public static void main(String[] args)
{ Scanner in = new Scanner(System.in);
 int a = nextInt();
 int b = nextInt();
 isPositive(<how do i refer to a and b here>);
}
public static bolean isPositive(Scanner in)
{ <how do I refer to a and b here, to check if (a - b) > 0 )
}

谢谢您!

mf98qq94

mf98qq941#

这里的一个问题是不能正确调用扫描仪。

int a = in.nextInt();
 int b = in.nextInt();

从这里您可以将从扫描仪获得的值传递到类ispositive中
公共静态布尔正(int a,int b){}

cx6n0qe3

cx6n0qe32#

你的代码中有几个问题。bmarkoe提到scanner类的错误使用(应该是 in.nextInt() 不是 nextInt() ).
这个 scanner 类实际上并不包含 a 以及 b . 相反,通过 a 以及 b 作为函数参数(示例如下)。还有,你拼错了 boolean 作为 bolean :)

public static boolean isPositive(int first, int second) {
  //some code
}

p、 s:您可以根据需要命名函数参数(例如 first 可以调用 f )

相关问题