我正在尝试从一个方法获取用户输入并在另一个方法中使用它。我对错误感到困惑,因为它们都是int类型。
public static void move()
{
System.out.println("What do you want to do?");
Scanner scan = new Scanner(System.in);
int userMove = scan.nextInt();
}
public static void usersMove(String playerName, int gesture)
{
int userMove = userMove.move(); //error is here
if (userMove == -1)
{
break;
}
答案 0 :(得分:6)
int userMove = userMove.menu();
userMove
这里是int
(原始数据类型)。你怎么能在那上面调用一个方法呢?
我猜你想要这样的东西: -
public static int move()
{
System.out.println("What do you want to do?");
Scanner scan = new Scanner(System.in);
int userMove = scan.nextInt();
return userMove;
}
public static void usersMove(String playerName, int gesture)
{
int userMove = move(); //Now error will go.
if (userMove == -1)
{
break;
}
答案 1 :(得分:0)
只是猜测,因为不清楚userMove是什么......但你似乎使用来自调用者中另一个方法的变量。你需要返回它来做那个
public static int move()
{
System.out.println("What do you want to do?");
Scanner scan = new Scanner(System.in);
return scan.nextInt();
}
public static void usersMove(String playerName, int gesture)
{
int userMove = move(); //error is here
if (userMove == -1)
{
break;
}