gpt4 book ai didi

Java - 剪刀石头布 - 如果错误则额外循环一次并计算获胜次数

转载 作者:行者123 更新时间:2023-12-02 10:50:59 25 4
gpt4 key购买 nike

我必须制作一个石头剪刀布游戏,该游戏会循环 5 次,如果输入无效数字,还会循环 5 次。如果错误的话,我还无法让它循环足够多,每当我输入错误的变量时,它都会循环额外的次数,但次数太多了,而我只需要 5 次正确的次数。它也不会计算玩家和计算机分数的数量。无论每个对手答对了多少,都只显示玩家获胜:1 和电脑获胜:1。请帮忙!这是我的代码

import java.util.*;
public class bmahipat_C5Lab1 {

public static void main(String[] args) {
Scanner reader = new Scanner (System.in);
int player = 0, computer = 0;
int computerScore = 0, playerScore = 0;
int loops = 0;

int rock = 1;
int paper = 2;
int scissors = 3;

for(int i=1; i<6;i++) {
computer = (int) (Math.random() * 3) + 1;
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();

if (player > scissors) {
System.out.println("Not a valid response");
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();
i = -1;
}

if (player == computer) {
System.out.println("Tie");
} else if (player == rock ) {
if(computer == paper){
System.out.println ("Player picked Rock, Computer picked Paper, Computer wins");
computerScore = +1;
} else if(computer == scissors) {
System.out.println ("Player picked Rock, Computer picked Scissors, Player wins");
playerScore = +1;
}
} else if (player == paper) {
if(computer == rock){
System.out.println ("Player picked Paper, Computer picked Rock , Player wins");
playerScore = +1;
} else if(computer == scissors) {
System.out.println ("Player picked Paper, Computer picked Scissors, Computer wins");
computerScore = +1;
}
} else if (player == scissors) {
if(computer == rock) {
System.out.println ("Player picked Scissors, Computer picked Rock , Computer wins");
computerScore = +1;
} else if(computer == paper) {
System.out.println ("Player picked Scissors, Computer picked Paper, Player wins");
playerScore = +1;
}
}
}

System.out.println("");
System.out.println("Computer Wins " + computerScore);
System.out.println("Player Wins " + playerScore);
}
}

最佳答案

差别很大

computerScore =+ 1; // This is computerScore = (+1) So it will be always 1

computerScore += 1; // this is equals to computerScore=computerScore+1;

的逻辑相同
  i = -1;

关于Java - 剪刀石头布 - 如果错误则额外循环一次并计算获胜次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191452/

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