无法从结果类型为 void 错误的方法返回值 [英] Cannot return a value from method whose result type is void error
本文介绍了无法从结果类型为 void 错误的方法返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从一种方法中获取用户输入并在另一种方法中使用它.我对无法从结果类型为 void 错误的方法返回值感到困惑,因为userMove"被定义为 int 类型.
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;
}
void
方法意味着它不返回任何内容(即没有 return 语句),除了 void
之外的任何内容都需要返回.
A void
method means it returns nothing (ie. no return statement), anything other then void
and a return is required.
这篇关于无法从结果类型为 void 错误的方法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文