-3

我试图从一种方法中获取用户输入并在另一种方法中使用它。我对这个错误感到困惑,因为它们都是 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;
    }
4

2 回答 2

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;
    }
于 2013-03-20T07:39:18.390 回答
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;
   }
于 2013-03-20T07:41:33.863 回答