英文:
Java, rock paper scissor program not working properly
问题
这是程序的说明...
"步骤1 从输入中读取两个玩家的名字(字符串)。从输入中读取回合数。如果值小于1,则继续读取回合数并提供错误消息。输出玩家的名字和回合数。提交以确认通过2个测试。
步骤2. 通过调用 rand.nextInt(3)
为玩家1生成0到2之间的随机值,然后为玩家2生成随机值。继续为两名玩家生成随机值,直到两个值不匹配。当值匹配时,输出“平局”。提交以确认通过3个测试。
步骤3. 识别本轮的赢家并输出消息。石头压剪刀,剪刀剪纸,纸覆盖石头。提交以确认通过6个测试。
步骤4. 添加循环来重复执行步骤2和3,直到完成所有回合。在所有回合完成后输出每位玩家的总胜利次数。提交以确认通过所有测试。"
为什么我得到的输出与预期不同...
import java.util.Scanner;
import java.util.Random;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int ROCK = 0;
final int PAPER = 1;
final int SCISSORS = 2;
Random rand = new Random();
int seed = scnr.nextInt();
rand.setSeed(seed);
/* 插入你的代码 */
String player1;
String player2;
int numOfRounds;
player1 = scnr.next();
player2 = scnr.next();
numOfRounds = scnr.nextInt();
while (numOfRounds < 1) {
System.out.println("Rounds must be > 0");
numOfRounds = scnr.nextInt();
}
System.out.println(player1 + " vs " + player2 + " for " + numOfRounds + " rounds");
int p1wins = 0;
int p2wins = 0;
for (int i = 0; i <= numOfRounds+1; ++i) {
int p1 = rand.nextInt(3);
int p2 = rand.nextInt(3);
if (p1 == p2) {
System.out.println("Tie");
}
else if (p1 == 0 && p2 == 1) {
System.out.println(player2 + " wins with paper");
p2wins+=1;
}
else if (p1 == 0 && p2 == 2) {
System.out.println(player1 + " wins with rock");
p1wins+=1;
}
else if (p1 == 1 && p2 == 0) {
System.out.println(player1 + " wins with paper");
p1wins+=1;
}
else if (p1 == 1 && p2 == 2) {
System.out.println(player2 + " wins with scissors");
p2wins+=1;
}
else if (p1 == 2 && p2 == 0) {
System.out.println(player2 + " wins with rock");
p2wins+=1;
}
else if (p1 == 2 && p2 == 1) {
System.out.println(player1 + " wins with scissors");
p1wins+=1;
}
}
if(p1wins < p2wins)
System.out.println(player2 + " wins " + p2wins + " and " + player1 + " wins " + p1wins);
else if(p2wins < p1wins)
System.out.println(player1 + " wins " + p1wins + " and " + player2 + " wins " + p2wins);
}
}
输入:
17 Randy Jenny 15
我的输出:
Randy wins with paper
Randy wins with paper
Randy wins with paper
Randy wins with paper
Randy wins with paper
Tie
Randy wins with paper
Jenny wins with scissors
Jenny wins with rock
Randy wins with rock
Jenny wins with scissors
Tie
Randy wins with rock
Jenny wins with paper
Tie
Randy wins 8 and Jenny wins 5
预期输出:
Randy wins with rock
Jenny wins with paper
Tie
Tie
Jenny wins with paper
Tie
Randy wins with paper
Randy wins 9 and Jenny wins 6
英文:
These are the instructions for this program...
"Step 1 Read two player names from input (String). Read number of rounds from input. Continue reading number of rounds if value is below one and provide an error message. Output player names and number of rounds. Submit for grading to confirm 2 tests pass.
Step 2. Generate random values (0 - 2) for player 1 followed by player 2 by calling rand.nextInt(3)
. Continue to generate random values for both players until both values do not match. Output "Tie" when the values match. Submit for grading to confirm 3 tests pass.
Step 3. Identify winner for this round and output a message. Rock crushes scissors, scissors cut paper, and paper covers rock. Submit for grading to confirm 6 tests pass.
Step 4. Add a loop to repeat steps 2 and 3 for the number of rounds. Output total wins for each player after all rounds are complete. Submit for grading to confirm all tests pass."
why am I getting a different output than expected...
import java.util.Scanner;
import java.util.Random;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int ROCK = 0;
final int PAPER = 1;
final int SCISSORS = 2;
Random rand = new Random();
int seed = scnr.nextInt();
rand.setSeed(seed);
/* Insert your code here */
String player1;
String player2;
int numOfRounds;
player1 = scnr.next();
player2 = scnr.next();
numOfRounds = scnr.nextInt();
while (numOfRounds < 1) {
System.out.println("Rounds must be > 0");
numOfRounds = scnr.nextInt();
}
System.out.println(player1 + " vs " + player2 + " for " + numOfRounds + " rounds");
int p1wins = 0;
int p2wins = 0;
for (int i = 0; i <= numOfRounds+1; ++i) {
int p1 = rand.nextInt(3);
int p2 = rand.nextInt(3);
if (p1 == p2) {
System.out.println("Tie");
}
else if (p1 == 0 && p2 == 1) {
System.out.println(player2 + " wins with paper");
p2wins+=1;
}
else if (p1 == 0 && p2 == 2) {
System.out.println(player1 + " wins with rock");
p1wins+=1;
}
else if (p1 == 1 && p2 == 0) {
System.out.println(player1 + " wins with paper");
p1wins+=1;
}
else if (p1 == 1 && p2 == 2) {
System.out.println(player2 + " wins with scissors");
p2wins+=1;
}
else if (p1 == 2 && p2 == 0) {
System.out.println(player2 + " wins with rock");
p2wins+=1;
}
else if (p1 == 2 && p2 == 1) {
System.out.println(player1 + " wins with scissors");
p1wins+=1;
}
}
if(p1wins < p2wins)
System.out.println(player2 + " wins " + p2wins + " and " + player1 + " wins " + p1wins);
else if(p2wins < p1wins)
System.out.println(player1 + " wins " + p1wins + " and " + player2 + " wins " + p2wins);
}
}
Input:
17 Randy Jenny 15
My Output:
Randy wins with paper
Randy wins with paper
Randy wins with paper
Randy wins with paper
Randy wins with paper
Tie
Randy wins with paper
Jenny wins with scissors
Jenny wins with rock
Randy wins with rock
Jenny wins with scissors
Tie
Randy wins with rock
Jenny wins with paper
Tie
Randy wins 8 and Jenny wins 5
Expected Output:
Randy wins with rock
Jenny wins with paper
Tie
Tie
Jenny wins with paper
Tie
Randy wins with paper
Randy wins 9 and Jenny wins 6
答案1
得分: 2
你的代码存在一些问题,我会逐一指出:
在这里,如果将声明和初始化赋值合并,可以提高代码的可读性:
``` lang-java
String player1 = scnr.next();
String player2 = scnr.next();
int numOfRounds = scnr.nextInt();
for (int i = 0; i <= numOfRounds+1; ++i) {
这个循环会多循环两次,如果你输入15作为回合数,i
的值会从0到16,这意味着循环会执行17次,多了两次。正确的写法应该是:
for (int i = 0; i < numOfRounds; ++i) {
int p1 = rand.nextInt(3);
int p2 = rand.nextInt(3);
if (p1 == p2) {
System.out.println("Tie");
}
这段代码会把“平局”(Tie)也计入回合次数,但预期输出明确不支持这一点(也不符合第2步的指令)。正确的做法是循环直到找到一个赢家(即两个随机值不相等):
int p1 = rand.nextInt(3);
int p2 = rand.nextInt(3);
while (p1 == p2) {
System.out.println("Tie");
p1 = rand.nextInt(3);
p2 = rand.nextInt(3);
}
通过更多的重构,你的代码可以变得更加易读和可重用(例如,将代码提取到可重用的方法中),但首先你应该确保它能正确运行。
<details>
<summary>英文:</summary>
Several issues with your code, going through it:
``` lang-java
String player1;
String player2;
int numOfRounds;
player1 = scnr.next();
player2 = scnr.next();
numOfRounds = scnr.nextInt();
Here it would increase readability if you merged declaration and initial assignment:
String player1 = scnr.next();
String player2 = scnr.next();
int numOfRounds = scnr.nextInt();
for (int i = 0; i <= numOfRounds+1; ++i) {
This will loop two times too much, if you enter 15 for the number of rounds, i
will go from 0 to 16, which is 17 entries into the loop - two to much.
correct would be:
for (int i = 0; i < numOfRounds; ++i) {
int p1 = rand.nextInt(3);
int p2 = rand.nextInt(3);
if (p1 == p2) {
System.out.println("Tie");
}
This does count a "Tie" also a round, which the expected output clearly does not support (also the instruction of step 2)
Correct would be a loop until a winner can be found (both random values are not equal):
int p1 = rand.nextInt(3);
int p2 = rand.nextInt(3);
while (p1 == p2) {
System.out.println("Tie");
p1 = rand.nextInt(3);
p2 = rand.nextInt(3);
}
With more refactoring your code could get even better read- and reusable (extracting code into reusable methodes etc.) but first you should get it running correctly.
答案2
得分: 0
首先,我会将代码分解成不同的部分。最好将win/lost
逻辑封装在单独的类中,例如enum Tool
。
public class Foo {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
Random random = new Random();
String p1 = getPlayerName(scan, 1);
String p2 = getPlayerName(scan, 2);
int totalRounds = getTotalRounds(scan);
System.out.format("%s vs %s for %d rounds\n", p1, p2, totalRounds);
int p1wins = 0;
int p2wins = 0;
for (int i = 0; i < totalRounds; i++) {
Tool p1Tool = Tool.random(random);
Tool p2Tool = Tool.random(random);
String res;
if (p1Tool == p2Tool) {
res = "Tie";
} else if (p1Tool.isWinner(p2Tool)) {
res = p1 + " wins";
p1wins++;
} else {
res = p2 + " wins";
p2wins++;
}
System.out.format("Round %d: %s (%s) vs %s (%s) - %s\n", i + 1,
p1, p1Tool.getName(), p2, p2Tool.getName(), res);
}
System.out.println("---");
System.out.format("%s (%d) vs %s (%d) - %s\n", p1, p1wins, p2, p2wins,
p1wins == p2wins ? "Tie" : (p1wins > p2wins ? p1 : p2) + " wins");
}
private static String getPlayerName(Scanner scan, int i) {
System.out.format("Enter player %d name: ", i);
return scan.next();
}
private static int getTotalRounds(Scanner scan) {
int res = 0;
while (res <= 0) {
System.out.print("Enter total rounds: ");
res = scan.nextInt();
}
return res;
}
@Getter
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
private enum Tool {
ROCK("rock", "scissors"),
PAPER("paper", "rock"),
SCISSORS("scissors", "paper");
private final String name;
private final String nameWin;
public final boolean isWinner(Tool tool) {
return nameWin.equals(tool.name);
}
public static Tool random(Random random) {
int id = random.nextInt(3);
return id == 0 ? ROCK : id == 1 ? PAPER : SCISSORS;
}
}
}
英文:
First of all I would decompose the code with a parts. It's better to encapsulate win/lost
logic within separate class e.g. enum Tool
.
public class Foo {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
Random random = new Random();
String p1 = getPlayerName(scan, 1);
String p2 = getPlayerName(scan, 2);
int totalRounds = getTotalRounds(scan);
System.out.format("%s vs %s for %d rounds\n", p1, p2, totalRounds);
int p1wins = 0;
int p2wins = 0;
for (int i = 0; i < totalRounds; i++) {
Tool p1Tool = Tool.random(random);
Tool p2Tool = Tool.random(random);
String res;
if (p1Tool == p2Tool) {
res = "Tie";
} else if (p1Tool.isWinner(p2Tool)) {
res = p1 + " wins";
p1wins++;
} else {
res = p2 + " wins";
p2wins++;
}
System.out.format("Round %d: %s (%s) vs %s (%s) - %s\n", i + 1,
p1, p1Tool.getName(), p2, p2Tool.getName(), res);
}
System.out.println("---");
System.out.format("%s (%d) vs %s (%d) - %s\n", p1, p1wins, p2, p2wins,
p1wins == p2wins ? "Tie" : (p1wins > p2wins ? p1 : p2) + " wins");
}
private static String getPlayerName(Scanner scan, int i) {
System.out.format("Enter player %d name: ", i);
return scan.next();
}
private static int getTotalRounds(Scanner scan) {
int res = 0;
while (res <= 0) {
System.out.print("Enter total rounds: ");
res = scan.nextInt();
}
return res;
}
@Getter
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
private enum Tool {
ROCK("rock", "scissors"),
PAPER("paper", "rock"),
SCISSORS("scissors", "paper");
private final String name;
private final String nameWin;
public final boolean isWinner(Tool tool) {
return nameWin.equals(tool.name);
}
public static Tool random(Random random) {
int id = random.nextInt(3);
return id == 0 ? ROCK : id == 1 ? PAPER : SCISSORS;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论