我如何使用 netbeans 在 java 每次掷骰子后询问用户是否想继续游戏?
How do I ask if the user would like to continue the game after each dice roll in java using netbeans?
我需要帮助解决这个问题:
用两个六面骰子玩掷骰子游戏。玩游戏的用户将掷两个骰子,将生成 1 到 6 之间的两个随机数。两个数字的总和将用于决定下一步。
如果总和为 2,3 或 12,则玩家获胜。如果总和为 7 或 11,则 he/she 输。如果总和为 4、5、6、8、9 或 10,则程序会自动再次掷骰子,直到玩家获胜或失败。
每次掷骰子后,系统都会提示玩家输入。玩家应该决定游戏是否应该继续。
每次掷骰子后也应显示输赢的游戏数量。
我已经设法让第一部分工作,但无法弄清楚如何提示用户是否希望继续或他们有多少游戏 won/lost
public class DiceGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
while (true) {
int dice1=(int)(Math.random()*6+1);
int dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum==2 || sum==3 || sum==12) {
System.out.println("Sorry with a " + sum + " you loose :(");
break;
}
else if(sum==7 || sum==11) {
System.out.println("With a " + sum + " you win :)");
break;
}
}
}
}
您可以使用 java 扫描仪 class 提示用户输入。
在你的 while 循环中,提示用户输入。您可能需要修改您的 while 循环条件。
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
根据用户的输入,使用'brake'退出循环。应该可以了。
我创建了一个 playAgainMessage
方法,其中 returns 是一个整数。这决定了用户是否会再次玩。看看:
import java.util.Scanner;
public class DiceGame {
private static final int PLAY = 1;
private static final int DO_NOT_PLAY = 2;
private static int choice = 1;
public static void main(String[] args) {
while (choice == PLAY) {
int dice1=(int)(Math.random()*6+1);
int dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum==2 || sum==3 || sum==12) {
System.out.println("Sorry with a " + sum + " you loose :(");
choice = playAgainMessage();
}
else if(sum==7 || sum==11) {
System.out.println("With a " + sum + " you win :)");
choice = playAgainMessage();
}
}
if (choice == DO_NOT_PLAY) {
System.out.println("Good bye...");
}
}
private static int playAgainMessage() {
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play again? '1' for yes, '2' for no");
int choice = input.nextInt();
input.close();
if(choice == PLAY){
System.out.println("Rolling...");
return PLAY;
} else {
return DO_NOT_PLAY;
}
}
}
您可以随时调用 playAgainMessage
方法,这样既美观又易于阅读。
这将一直播放,直到用户输入“1”以外的内容。尝试 运行 以查看准确的输出。
您可以使用 do{}while();
循环,它会播放一次,然后要求用户再次播放:
Scanner sc = new Scanner(System.in);
String input = "";
do {
int // ...
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
// ...
} else if (sum == 7 || sum == 11) {
// ...
}
System.out.println("If you want to continue, write 'y' : ");
input = sc.nextLine();
} while (input.equalsIgnoreCase("y"));
扫描仪可用于提示用户询问he/she是否要继续。
你甚至可以跟踪没有。掷骰子的次数。
import java.util.Scanner;
public class DiceGame {
public static int attempt = 1;
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int dice1 = (int) (Math.random() * 6 + 1);
int dice2 = (int) (Math.random() * 6 + 1);
int sum = dice1 + dice2;
while (true) {
System.out.println();
System.out.println("Rolling dice for " + attempt + " time!");
dice1 = (int) (Math.random() * 6 + 1);
dice2 = (int) (Math.random() * 6 + 1);
sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
System.out.println("Sorry with a " + sum + " you loose :(!");
System.out.println();
break;
} else if (sum == 7 || sum == 11) {
System.out.println("With a " + sum + " you win :)!");
System.out.println();
break;
}
System.out.println();
System.out.println("Do you wish to continue? Press 'y' for YES or ANY key for EXIT");
if (!scanner.next().equalsIgnoreCase("y")) {
break;
}
attempt++;
}
System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
}
}
EDIT:
如果您想在总和为 4 5 6 8 9 10 时自动掷骰子,那么您将不再需要 Scanner 即用户输入是否继续。
这是相同的解决方案。
public class DiceGame {
public static int attempt = 1;
public static void main(String[] args) {
int dice1 = 0;
int dice2 = 0;
int sum = 0;
while (true) {
System.out.println();
System.out.println("Rolling dice for " + attempt + " time!");
dice1 = (int) (Math.random() * 6 + 1);
dice2 = (int) (Math.random() * 6 + 1);
sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
System.out.println("Sorry with a " + sum + " you loose :(!");
System.out.println();
break;
} else if (sum == 7 || sum == 11) {
System.out.println("With a " + sum + " you win :)!");
System.out.println();
break;
// this will roll the dices automatically
// when sum is 4, 5, 6, 8, 9 or 10
} else {
System.out.println();
System.out.println("With " + sum + " dices are rolled again automatically!!");
attempt++;
}
}
System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
}
}
示例 运行
Rolling dice for 1 time!
Roll: total = 4
With a 4, dices are rolled again automatically!!
Rolling dice for 2 time!
Roll: total = 6
With a 6, dices are rolled again automatically!!
Rolling dice for 3 time!
Roll: total = 2
Sorry with a 2 you loose :(!
Thanks for playing dice game, you rolled the dice 3 times!
我需要帮助解决这个问题:
用两个六面骰子玩掷骰子游戏。玩游戏的用户将掷两个骰子,将生成 1 到 6 之间的两个随机数。两个数字的总和将用于决定下一步。
如果总和为 2,3 或 12,则玩家获胜。如果总和为 7 或 11,则 he/she 输。如果总和为 4、5、6、8、9 或 10,则程序会自动再次掷骰子,直到玩家获胜或失败。
每次掷骰子后,系统都会提示玩家输入。玩家应该决定游戏是否应该继续。
每次掷骰子后也应显示输赢的游戏数量。
我已经设法让第一部分工作,但无法弄清楚如何提示用户是否希望继续或他们有多少游戏 won/lost
public class DiceGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
while (true) {
int dice1=(int)(Math.random()*6+1);
int dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum==2 || sum==3 || sum==12) {
System.out.println("Sorry with a " + sum + " you loose :(");
break;
}
else if(sum==7 || sum==11) {
System.out.println("With a " + sum + " you win :)");
break;
}
}
}
}
您可以使用 java 扫描仪 class 提示用户输入。
在你的 while 循环中,提示用户输入。您可能需要修改您的 while 循环条件。
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
根据用户的输入,使用'brake'退出循环。应该可以了。
我创建了一个 playAgainMessage
方法,其中 returns 是一个整数。这决定了用户是否会再次玩。看看:
import java.util.Scanner;
public class DiceGame {
private static final int PLAY = 1;
private static final int DO_NOT_PLAY = 2;
private static int choice = 1;
public static void main(String[] args) {
while (choice == PLAY) {
int dice1=(int)(Math.random()*6+1);
int dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum==2 || sum==3 || sum==12) {
System.out.println("Sorry with a " + sum + " you loose :(");
choice = playAgainMessage();
}
else if(sum==7 || sum==11) {
System.out.println("With a " + sum + " you win :)");
choice = playAgainMessage();
}
}
if (choice == DO_NOT_PLAY) {
System.out.println("Good bye...");
}
}
private static int playAgainMessage() {
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play again? '1' for yes, '2' for no");
int choice = input.nextInt();
input.close();
if(choice == PLAY){
System.out.println("Rolling...");
return PLAY;
} else {
return DO_NOT_PLAY;
}
}
}
您可以随时调用 playAgainMessage
方法,这样既美观又易于阅读。
这将一直播放,直到用户输入“1”以外的内容。尝试 运行 以查看准确的输出。
您可以使用 do{}while();
循环,它会播放一次,然后要求用户再次播放:
Scanner sc = new Scanner(System.in);
String input = "";
do {
int // ...
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
// ...
} else if (sum == 7 || sum == 11) {
// ...
}
System.out.println("If you want to continue, write 'y' : ");
input = sc.nextLine();
} while (input.equalsIgnoreCase("y"));
扫描仪可用于提示用户询问he/she是否要继续。
你甚至可以跟踪没有。掷骰子的次数。
import java.util.Scanner;
public class DiceGame {
public static int attempt = 1;
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int dice1 = (int) (Math.random() * 6 + 1);
int dice2 = (int) (Math.random() * 6 + 1);
int sum = dice1 + dice2;
while (true) {
System.out.println();
System.out.println("Rolling dice for " + attempt + " time!");
dice1 = (int) (Math.random() * 6 + 1);
dice2 = (int) (Math.random() * 6 + 1);
sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
System.out.println("Sorry with a " + sum + " you loose :(!");
System.out.println();
break;
} else if (sum == 7 || sum == 11) {
System.out.println("With a " + sum + " you win :)!");
System.out.println();
break;
}
System.out.println();
System.out.println("Do you wish to continue? Press 'y' for YES or ANY key for EXIT");
if (!scanner.next().equalsIgnoreCase("y")) {
break;
}
attempt++;
}
System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
}
}
EDIT:
如果您想在总和为 4 5 6 8 9 10 时自动掷骰子,那么您将不再需要 Scanner 即用户输入是否继续。
这是相同的解决方案。
public class DiceGame {
public static int attempt = 1;
public static void main(String[] args) {
int dice1 = 0;
int dice2 = 0;
int sum = 0;
while (true) {
System.out.println();
System.out.println("Rolling dice for " + attempt + " time!");
dice1 = (int) (Math.random() * 6 + 1);
dice2 = (int) (Math.random() * 6 + 1);
sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
if (sum == 2 || sum == 3 || sum == 12) {
System.out.println("Sorry with a " + sum + " you loose :(!");
System.out.println();
break;
} else if (sum == 7 || sum == 11) {
System.out.println("With a " + sum + " you win :)!");
System.out.println();
break;
// this will roll the dices automatically
// when sum is 4, 5, 6, 8, 9 or 10
} else {
System.out.println();
System.out.println("With " + sum + " dices are rolled again automatically!!");
attempt++;
}
}
System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
}
}
示例 运行
Rolling dice for 1 time!
Roll: total = 4
With a 4, dices are rolled again automatically!!
Rolling dice for 2 time!
Roll: total = 6
With a 6, dices are rolled again automatically!!
Rolling dice for 3 time!
Roll: total = 2
Sorry with a 2 you loose :(!
Thanks for playing dice game, you rolled the dice 3 times!