gpt4 book ai didi

java - 石头剪刀布,又玩了?

转载 作者:行者123 更新时间:2023-12-01 12:22:39 26 4
gpt4 key购买 nike

对于我的作业,我应该让程序成为剪刀石头布,我发现我的主要问题是我无法让程序再次正确运行或让程序正确计算游戏分数,请帮助我'我疯狂地想弄清楚这个问题?!

//Tayler Dorsey

import java.util.Random;

import java.util.Scanner;

public class PRS {

static Scanner keyboard = new Scanner(System. in );

public static void instructions() {
System.out.println("This is the popular game of paper, rock, scissors. Enter your\nchoice by typing the word \"paper\", the word \"rock\" or the word\n\"scissors\". The computer will also make a choice from the three\noptions. After you have entered your choice, the winner of the\ngame will be determined according to the following rules:");
System.out.println("Paper wraps rock (paper wins)\nRock breaks scissors (rock wins)\nScissors cuts paper (scissors wins)");
System.out.println("If both you and the computer enter the same choice, then the game is tied.");
}

public static int playGame() {

int ties = 0, wins = 0, losts = 0;

String userchoice, computerchoice;

System.out.println("Enter your choice: ");

userchoice = keyboard.next();

computerchoice = computerChoose();

System.out.println("You entered: " + userchoice);

System.out.println("Computer choose: " + computerchoice);

if ((userchoice.equals("paper") && computerchoice.equals("paper")) || (userchoice.equals("rock") && computerchoice.equals("rock")) || (userchoice.equals("scissors") && computerchoice.equals("scissors"))) {
System.out.println("IT'S A TIE!");
ties++;
return 3;
} else if ((userchoice.equals("paper") && computerchoice.equals("rock")) || (userchoice.equals("rock") && computerchoice.equals("scissors")) || (userchoice.equals("scissors") && computerchoice.equals("paper"))) {
System.out.println("YOU WIN!");
wins++;
return 1;
} else {
System.out.println("YOU LOSE!");
losts++;
return 2;
}
}

public static String computerChoose() {
Random generator = new Random();
String[] answer = new String[3];
answer[0] = "paper";
answer[1] = "rock";
answer[2] = "scissors";
return answer[generator.nextInt(3)];
}

public static void main(String[] args) {
String play;
Scanner keyboard = new Scanner(System. in );
System.out.println("The Game of Paper, Rock, Scissors");
System.out.println("Do you need instructions (y or n)?");
String help = keyboard.nextLine();
if (help.equals("y")) instructions();

int result = playGame();
System.out.println("Play again (y or n)?");
play = keyboard.nextLine();
if (play.equals("y"));

else {
int count = 0, wins = 0, losts = 0, ties = 0;
System.out.println("Games played: " + count);
System.out.println("Wins for you: " + wins);
System.out.println("Wins for me: " + losts);
System.out.println("Tied games: " + ties);
}
do {} while (play == "y"); {
playGame();
int count = 0;
count++;
}
}
}

最佳答案

这里有两个问题:

  1. 代码不在 do-while 循环内,而是在其之后
  2. 应使用 equals 检查字符串是否相等,而不是使用 ==

所以:

int count = 0;
do { // Note the code inside the do-while block
playGame();
count++;
} while (play.equals("y")); // Note the use of equals

关于java - 石头剪刀布,又玩了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26564846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com