用java语言编写石头剪刀布获胜法,剪刀石头布Java

I'm new to programming and I'm trying to write a very simple Rock, Paper, Scissors game in Java. It will compile and run fine, but I am looking to say something like "Invalid move. Try again." or something along those lines for when the user (personPlay) does not enter a correct character (r, p, or s). What would be the best way to do so? For example, if you enter a "q", it should print "Invalid move." Thank you so much in advance!

// *************

// 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.");

}

}

解决方案

I would recommend making Rock, Paper and Scissors objects. The objects would have the logic of both translating to/from Strings and also "knowing" what beats what. The Java enum is perfect for this.

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

}

}

The parseType method can return null if the String is not a valid type. And you code can check if the value is null and if so, print "invalid try again" and loop back to re-read the Scanner.

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");

}

}

Furthermore, your type enum can determine what beats what by having each Type object know:

public enum Type{

//...

//each type will implement this method differently

public abstract boolean beats(Type other);

}

each type will implement this method differently to see what beats what:

ROCK{

@Override

public boolean beats(Type other){

return other == SCISSOR;

}

}

...

Then in your code

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!!");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值