gpt4 book ai didi

java - Java 石头剪刀布游戏

转载 作者:行者123 更新时间:2023-12-01 18:42:50 29 4
gpt4 key购买 nike

我是编程新手,我正在尝试用 Java 编写一个非常简单的石头剪刀布游戏。它将编译并运行良好,但我希望说“无效的移动。再试一次。”或当用户 (personPlay) 未输入正确的字符(r、p 或 s)时的类似内容。最好的方法是什么?例如,如果您输入“q”,它应该打印“无效移动”。提前非常感谢您!

// *************
// Rock.java
// *************

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


public class Rock
{
public static void main(String[] args)
{
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay = ""; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine
//computer's play
String response;


Scanner scan = new Scanner(System.in);
Random generator = new Random();

System.out.println("Hey, let's play Rock, Paper, Scissors!\n" +
"Please enter a move.\n" + "Rock = R, Paper" +
"= P, and Scissors = S.");

System.out.println();

//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3)+1;

//Translate computer's randomly generated play to
//string using if //statements

if (computerInt == 1)
computerPlay = "R";
else if (computerInt == 2)
computerPlay = "P";
else if (computerInt == 3)
computerPlay = "S";


//Get player's play from input-- note that this is
// stored as a string
System.out.println("Enter your play: ");
personPlay = scan.next();

//Make player's play uppercase for ease of comparison
personPlay = personPlay.toUpperCase();

//Print computer's play
System.out.println("Computer play is: " + computerPlay);


//See who won. Use nested ifs

if (personPlay.equals(computerPlay))
System.out.println("It's a tie!");
else if (personPlay.equals("R"))
if (computerPlay.equals("S"))
System.out.println("Rock crushes scissors. You win!!");
else if (computerPlay.equals("P"))
System.out.println("Paper eats rock. You lose!!");
else if (personPlay.equals("P"))
if (computerPlay.equals("S"))
System.out.println("Scissor cuts paper. You lose!!");
else if (computerPlay.equals("R"))
System.out.println("Paper eats rock. You win!!");
else if (personPlay.equals("S"))
if (computerPlay.equals("P"))
System.out.println("Scissor cuts paper. You win!!");
else if (computerPlay.equals("R"))
System.out.println("Rock breaks scissors. You lose!!");
else
System.out.println("Invalid user input.");
}

}

最佳答案

我建议制作石头、剪刀、布的物体。这些对象将具有与字符串之间进行转换的逻辑,并且还“知道”什么击败什么。 Java 枚举非常适合此目的。

public enum Type{

ROCK, PAPER, SCISSOR;

public static Type parseType(String value){
//if /else logic here to return either ROCK, PAPER or SCISSOR

//if value is not either, you can return null
}
}

如果 String 不是有效类型,则 parseType 方法可以返回 null。您的代码可以检查该值是否为空,如果是,则打印“无效重试”并循环返回以重新读取扫描仪。

Type person=null;

while(person==null){
System.out.println("Enter your play: ");
person= Type.parseType(scan.next());
if(person ==null){
System.out.println("invalid try again");
}
}

此外,您的类型枚举可以通过让每个 Type 对象知道来确定什么优于什么:

public enum Type{

//...

//each type will implement this method differently
public abstract boolean beats(Type other);


}

每种类型都会以不同的方式实现此方法,以查看哪种类型优于哪种:

ROCK{

@Override
public boolean beats(Type other){
return other == SCISSOR;

}
}

...

然后在你的代码中

 Type person, computer;
if (person.equals(computer))
System.out.println("It's a tie!");
}else if(person.beats(computer)){
System.out.println(person+ " beats " + computer + "You win!!");
}else{
System.out.println(computer + " beats " + person+ "You lose!!");
}

关于java - Java 石头剪刀布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19204872/

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