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

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

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

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

谢谢您!

mf98qq94

mf98qq941#

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

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

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

cx6n0qe3

cx6n0qe32#

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

  1. public static boolean isPositive(int first, int second) {
  2. //some code
  3. }

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

相关问题