gpt4 book ai didi

java - 当用户选择停止时,如何打印用户的赢、输、平数

转载 作者:行者123 更新时间:2023-11-29 05:17:04 25 4
gpt4 key购买 nike

此时,程序显示两种选择并打印一条语句,表明用户赢了、计算机赢了还是平局。继续玩直到用户选择停止,然后打印用户赢的次数,损失, 和关系.

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissor {

public static void main(String[] args) {

final int MAX = 3;
int randomNumber;
int userChoice;
int computerChoice;
int choice;
Scanner input = new Scanner(System.in);
Random rand = new Random();//random number generated by computer

String str, another = "y";

Scanner scan = new Scanner(System.in);
int rounds = 0;
userChoice = 0;

while (another.equalsIgnoreCase("y")) {// allows y or Y
System.out.print("enter 1= rock 2= paper 3 = scissor(0 to quit):");
rounds++;
userChoice = input.nextInt();
computerChoice = rand.nextInt(MAX) + 1;
System.out.println("computer picked " + computerChoice);

int lose = 0;
int win = 0;
int tie = 0;
int computerScore = 0, userScore = 0;


if (userChoice == 1 || userChoice == 2 || userChoice == 3) {
if (userChoice == computerChoice) {
System.out.println("Tie Game!");
System.out.println();
tie++;
//rounds++;
} else if (userChoice == 1) {

if (computerChoice == 2) {
//rock is covered by paper
System.out.println(" you lose");
lose++;
computerScore++;
//rounds++;
} else if (computerChoice == 3) {
//rock smashes scissors
System.out.println(" you win");
win++;
userScore++;
//rounds++;
}
} else if (userChoice == 2) {
if (computerChoice == 3) {
//paper is cut by scissors
System.out.println(" you lose");
lose++;
computerScore++;
//rounds++;
} else if (computerChoice == 1) {
//paper covers rock
System.out.println(" you win");
win++;
userScore++;
//rounds++;
}
} else if (userChoice == 3) {

if (computerChoice == 1) {
//scissors are smashed by rock
System.out.println(" you lose");
lose++;
computerScore++;
//rounds++;
} else if (computerChoice == 2) {
//scissors cut paper
System.out.println(" you win");
win++;
userScore++;
//rounds++;
}
}

} else {
System.out.println("invalid number");
}
System.out.println();
System.out.println("User wins: " + win + " User loses: " + lose + "User ties " + tie);
System.out.print("Do you want to play again (y/n)? ");
another = scan.nextLine();
}
}
}

最佳答案

通过下面的代码,

import java.util.Scanner;
import java.util.Random;

公共(public)类 RockPaperScissor {

 public static void main(String[] args) {

final int MAX = 3;
int randomNumber = 0;
int userChoice = 0;
int computerChoice = 0;
int choice = 0;
int rounds = 0;
int lose = 0;
int win = 0;
int tie = 0;
int computerScore = 0;
int userScore = 0;
Scanner input = new Scanner(System.in);
Random rand = new Random();//random number generated by computer
String str, another = "y";

userChoice = 0;

while (another.equalsIgnoreCase("y")) {// allows y or Y
System.out.print("enter 1= rock 2= paper 3 = scissor(0 to quit):");
rounds++;
userChoice = Integer.parseInt(input.nextLine());

if(userChoice == 0){
System.out.print("Do you want to play again (y/n)? ");
another = input.nextLine();
} else if (userChoice > 0 && userChoice < 4){
computerChoice = rand.nextInt(MAX) + 1;
System.out.println("computer picked " + computerChoice);

if (userChoice == computerChoice) {
System.out.println("Tie Game!");
System.out.println();
tie++;
} else if (userChoice == 1) {

if (computerChoice == 2) {
//rock is covered by paper
System.out.println(" you lose");
lose++;
computerScore++;
} else if (computerChoice == 3) {
//rock smashes scissors
System.out.println(" you win");
win++;
userScore++;
}
} else if (userChoice == 2) {
if (computerChoice == 3) {
//paper is cut by scissors
System.out.println(" you lose");
lose++;
computerScore++;
} else if (computerChoice == 1) {
//paper covers rock
System.out.println(" you win");
win++;
userScore++;
}
} else if (userChoice == 3) {

if (computerChoice == 1) {
//scissors are smashed by rock
System.out.println(" you lose");
lose++;
computerScore++;
} else if (computerChoice == 2) {
//scissors cut paper
System.out.println(" you win");
win++;
userScore++;
}
}
}else{
System.out.println("invalid number");
}
System.out.println("computer score: " + computerScore + " You Score: " + userScore + " Rounds: " + rounds+"\n");
}
}

我已经从您的原始代码中删除了扫描和一个 while 循环并定义了现有条件。

祝你好运!!!

关于java - 当用户选择停止时,如何打印用户的赢、输、平数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172857/

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