无法从结果类型为void错误的方法返回值 [英] Cannot return a value from method whose result type is void error

查看:596
本文介绍了无法从结果类型为void错误的方法返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个方法中获取用户输入,并在另一个方法中使用它。我很困惑,因为userMove被定义为int类型,所以无法从结果类型为void错误的方法返回值。

I am trying to take the user input from one method and use it in another. I am confused about the cannot return a value from method whose result type is void error because "userMove" is defined as type int.

public static void 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();

    if (userMove == -1)
    {
        break;
    }


推荐答案

当您返回 int 时,将 void 更改为 int

Simply change void to int as you're returning an 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;
}

A void 意味着它不返回任何东西(即没有返回语句),任何其他 void 并且需要返回。

A void method means it returns nothing (ie. no return statement), anything other then void and a return is required.

这篇关于无法从结果类型为void错误的方法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆