试图弄清楚如何将总分返回到 main 以打印出来。它正在显示但未存储在变量中
Trying to figure out how to to bring the total score back up to main to print out. It is being displayed but not stored in the variable
我正在想办法将总分返回到 main 以打印出来。它正在显示但未存储在变量中
这是我 code.I 中最重要的方法,我正在尝试弄清楚如何 return 每个玩家的最终得分,即使这些方法是嵌套的。这应该就像游戏 Price Is Right 一样。三名玩家都在转动轮子,试图尽可能接近 100。如果他们过去了,他们就出局了。我没有包括我的主要方法,但我只是一直在为每个人调用他们的 methid player()。我已经使用了这种方法,我只需要弄清楚如何将值发送到 main。
public static int player() {
Scanner inputScanner = new Scanner(System.in); //asks user if they are ready
String userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
int total = 0;
total += spinWheel(); //method to spin wheel
if (total < 100) {
System.out.println("Would you like to spin again?"); //if < 100 they have the chance to spin again
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) { /asks them if they want to spin
total += spinWheelTwice();
System.out.println("Final Score is: " + total); //adds to the earlier spin
} // I want to store the final score in a variable that I can pass back up to main
}
}
}
在你的调用方法中(main
我认为你使用),只需为 player()
中的 return 声明一个变量,即 int total = player();
.
而在player()
中,您需要return调用方法的金额。但首先,我会在 player()
.
的最顶部声明 total
您只需要 return 块末尾的 total
变量。
public static int player() {
int total = 0;
Scanner inputScanner = new Scanner(System.in);
String userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheel();
if (total < 100) {
System.out.println("Would you like to spin again?");
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheelTwice();
System.out.println("Final Score is: " + total);
}
}
}
return total;
}
然后您可以根据自己的选择在 main
中使用 total
变量。
编辑:
根据下面的评论,如果您希望在总分小于 100 时告知用户他们已经退出游戏,或许可以考虑在您告知总分的位置添加 if...else
:
if (userInput.equals("yes")) {
total += spinWheelTwice();
if (total < 100) {
System.out.println("You are out of the game");
} else {
System.out.println("Final Score is: " + total);
}
}
如果用户没有第二次输入 "yes"(即使他们第一次没有输入),您也可能需要它,但如果是这样,则将其放在 else
子句到第二个嵌套 if
.
示例:
public static int player() {
int total = 0;
Scanner inputScanner = new Scanner(System.in);
String userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheel();
if (total < 100) {
System.out.println("Would you like to spin again?");
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheelTwice();
if (total < 100) {
System.out.println("You are out of the game");
} else {
System.out.println("Final Score is: " + total);
}
} else {
System.out.println("You are out of the game");
}
}
}
return total;
}
您的问题是您在 if
范围内声明了 total
变量,这意味着变量 total
无法从该范围外访问。如果你想return它,你需要在范围外声明total
。
if (userInput.equals("yes")) {
int total = 0;
// ...
}
return total; // this is unreachable
应该是
int total = 0;
if (userInput.equals("yes")) {
// ...
}
return total;
为了回答你的问题,
public static int player() {
System.out.println("Are you ready? (yes/no)");
Scanner inputScanner = new Scanner(System.in);
String userInput = inputScanner.nextLine();
int total = 0;
if (userInput.equals("yes")) {
total = spinWheel();
System.out.println("You spun " + total);
if (total < 100) {
System.out.println("Would you like to spin again?");
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheelTwice();
}
}
}
return total;
}
public static void main(String... args) {
int total = YourClassName.player();
System.out.println("Final Score is: " + total);
}
这样的代码勾起了美好的回忆
我正在想办法将总分返回到 main 以打印出来。它正在显示但未存储在变量中
这是我 code.I 中最重要的方法,我正在尝试弄清楚如何 return 每个玩家的最终得分,即使这些方法是嵌套的。这应该就像游戏 Price Is Right 一样。三名玩家都在转动轮子,试图尽可能接近 100。如果他们过去了,他们就出局了。我没有包括我的主要方法,但我只是一直在为每个人调用他们的 methid player()。我已经使用了这种方法,我只需要弄清楚如何将值发送到 main。
public static int player() {
Scanner inputScanner = new Scanner(System.in); //asks user if they are ready
String userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
int total = 0;
total += spinWheel(); //method to spin wheel
if (total < 100) {
System.out.println("Would you like to spin again?"); //if < 100 they have the chance to spin again
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) { /asks them if they want to spin
total += spinWheelTwice();
System.out.println("Final Score is: " + total); //adds to the earlier spin
} // I want to store the final score in a variable that I can pass back up to main
}
}
}
在你的调用方法中(main
我认为你使用),只需为 player()
中的 return 声明一个变量,即 int total = player();
.
而在player()
中,您需要return调用方法的金额。但首先,我会在 player()
.
total
您只需要 return 块末尾的 total
变量。
public static int player() {
int total = 0;
Scanner inputScanner = new Scanner(System.in);
String userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheel();
if (total < 100) {
System.out.println("Would you like to spin again?");
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheelTwice();
System.out.println("Final Score is: " + total);
}
}
}
return total;
}
然后您可以根据自己的选择在 main
中使用 total
变量。
编辑:
根据下面的评论,如果您希望在总分小于 100 时告知用户他们已经退出游戏,或许可以考虑在您告知总分的位置添加 if...else
:
if (userInput.equals("yes")) {
total += spinWheelTwice();
if (total < 100) {
System.out.println("You are out of the game");
} else {
System.out.println("Final Score is: " + total);
}
}
如果用户没有第二次输入 "yes"(即使他们第一次没有输入),您也可能需要它,但如果是这样,则将其放在 else
子句到第二个嵌套 if
.
示例:
public static int player() {
int total = 0;
Scanner inputScanner = new Scanner(System.in);
String userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheel();
if (total < 100) {
System.out.println("Would you like to spin again?");
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheelTwice();
if (total < 100) {
System.out.println("You are out of the game");
} else {
System.out.println("Final Score is: " + total);
}
} else {
System.out.println("You are out of the game");
}
}
}
return total;
}
您的问题是您在 if
范围内声明了 total
变量,这意味着变量 total
无法从该范围外访问。如果你想return它,你需要在范围外声明total
。
if (userInput.equals("yes")) {
int total = 0;
// ...
}
return total; // this is unreachable
应该是
int total = 0;
if (userInput.equals("yes")) {
// ...
}
return total;
为了回答你的问题,
public static int player() {
System.out.println("Are you ready? (yes/no)");
Scanner inputScanner = new Scanner(System.in);
String userInput = inputScanner.nextLine();
int total = 0;
if (userInput.equals("yes")) {
total = spinWheel();
System.out.println("You spun " + total);
if (total < 100) {
System.out.println("Would you like to spin again?");
userInput = inputScanner.nextLine();
if (userInput.equals("yes")) {
total += spinWheelTwice();
}
}
}
return total;
}
public static void main(String... args) {
int total = YourClassName.player();
System.out.println("Final Score is: " + total);
}
这样的代码勾起了美好的回忆