Java scanner的参数,在函数中传递Scanner对象作为参数的基本语法

Here is what I wrote which is pretty basic :

import java.util.Scanner;

public class Projet {

/**

* @param args

* @param Scanner

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Enter a digit");

Scanner in = new Scanner(System.in);

getChoice(Scanner);

in.close();

}

public static int getChoice(Scanner n){

n = in.nextInt();

return n;

}

}

What seems to be wrong here ? I had it working earlier, I had to pass the Scanner type and argument name as a parameter to the function... and simply call that function in the main using Scanner type and argument as an argument to the function ?

-----EDIT-----

New Code below for below that will need it :

import java.util.Scanner;

public class Projet {

/**

* @param args

* @param Scanner

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Enter a digit");

Scanner in = new Scanner(System.in);

System.out.println(getChoice(in));

in.close();

}

public static int getChoice(Scanner in){

return in.nextInt();

}

}

@rgettman Thanks !

解决方案

You need to pass the actual variable name in when you call the method, not the class name Scanner.

getChoice(in);

instead of

getChoice(Scanner);

Incidentally, your getChoice method won't compile as shown. Just return what the scanner returns, which is an int, as you declared getChoice to return an int:

public static int getChoice(Scanner n){

return n.nextInt();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值