剪刀石头布游戏,可以做到判定一局游戏的胜负,
若是想要玩三局 甚至五局,加 for(int i=1;i<=3; i++)
但总是达不到目的。求教各位大神,这个for循环加入到那里合适?
import java.util.Scanner;
import java.util.Random;
//剪刀,石头,布
public static void main(String[] args) {
int count = 0; //玩游戏的初始值
int computerScore = 0; //计算机得分初始值
int humanScore = 0; //人类得分初始值
Scanner s = new Scanner(System.in);
Random r = new Random();//计算机随机产生选择
// 开始循环
while(true) {
//人类选择
System.out.println("Please enter your choice: paper, scissors or stone. ");
String human = s.nextLine();
// 取得int格式的计算机选择
int number = r.nextInt(3);
String computer;
//将计算机选择的int转换为string格式且对应 paper scissors stone
if(number == 0) {
computer = "paper";
}else if(number == 1){
computer = "scissors";
}else {
computer = "stone";
}
System.out.println("computer choose: "+computer);
// 判定谁赢并计算得分
if(computer.equals(human)) {
System.out.println("Draw! ");
count += 1;
}else if(computer.equals("paper") && human.equals("stone")) {
System.out.println("computer wins! ");
count += 1;
computerScore +=1;
}else if(computer.equals("paper") && human.equals("scissors")) {
System.out.println("human wins! ");
count += 1;
humanScore += 1;
}else if(computer.equals("scissors") && human.equals("paper")) {
System.out.println("computer wins! ");
count += 1;
computerScore += 1;
}else if(computer.equals("scissors") && human.equals("stone")) {
System.out.println("human wins! ");
count += 1;
humanScore += 1;
}else if(computer.equals("stone") && human.equals("scissors")) {
System.out.println("computer wins! ");
count += 1;
computerScore += 1;
}else if(computer.equals("stone") && human.equals("paper")) {
System.out.println("human wins! ");
count += 1;
humanScore += 1;
}
System.out.println("Scores: human: "+humanScore);
System.out.println(" computer: "+computerScore);
if(humanScore > computerScore) {
System.out.println("human wins the final game!");
}else if (humanScore < computerScore) {
System.out.println("computer wins the final game!");
}else if (humanScore == computerScore) {
System.out.println("Draw!");
}
System.out.println("you totally play "+count+" times!");
}
}
结果如下:
Please enter your choice: paper, scissors or stone.
paper
computer choose: paper
Draw!
Scores: human: 0
computer: 0
Draw!
you totally play 1 times!
Please enter your choice: paper, scissors or stone.