我正在用java编写一个石头剪刀游戏,但有些事情我想不出来。首先,我想让用户可以输入“Rock”或“paper”,而不是1、2和3,但我想不出来。其次,我应该使用嵌套的if-else语句,但我也不知道如何使用我所做的。我的代码如下
导入Java . util . scanner;公共类RockGame {
private final static int ROCK=1;
private final static int PAPER =2;
private final static int SCISSOR =3;
private static Scanner key;
public static void main(String args[]){
int playerOneScore = 0;
int computerScore = 0;
int userPlay, computerPlay;
String val = key.nextLine().toLowerCase();
key = new Scanner(System.in);
while(playerOneScore <2 && computerScore <2){
System.out.println("Choose 1 for rock, 2 for paper, and 3 for sciscors!");
userPlay = key.nextInt();
computerPlay = (int)(Math.random()*3) +1;
if(val.equals("rock"))
userPlay = ROCK;
else if (val.equals("paper"))
userPlay =PAPER;
else if (val.equals("scissors"))
userPlay=SCISSOR;
if (val.equals("rock"))
computerPlay = ROCK;
else if (val.equals("paper"))
computerPlay =PAPER;
else if (val.equals("scissors"))
computerPlay=SCISSOR;
if (computerPlay ==ROCK && userPlay==SCISSOR ){
System.out.println("The computer chose rock, you chose scissors.\n You lose!");
computerScore++;
}
if (computerPlay ==ROCK && userPlay==PAPER ){
System.out.println("You computer chose rock, you chose paper.\n You win!");
playerOneScore++;
}
if (computerPlay ==PAPER && userPlay==SCISSOR ){
System.out.println("The computer chose scissors, you chose paper.\n You win!");
playerOneScore++;
}
if (computerPlay ==PAPER && userPlay==ROCK ){
System.out.println("The computer chose paper and you chose rock. \n You lose!");
computerScore++;
}
if (computerPlay ==SCISSOR && userPlay==ROCK ){
System.out.println("The computer chose scissors and you chose rock. \n You win!");
playerOneScore++;
}
if (computerPlay ==SCISSOR && userPlay==PAPER ){
System.out.println("The computer chose scissors and you chose paper. \n You lose!");
computerScore++;
}
if (computerPlay == userPlay ){
System.out.println("The computer chose the same thing you did! \n Tie!");
}
}
if(computerScore > playerOneScore)
System.out.println("Computer win score is: - "+ computerScore + " -" + playerOneScore );
else
System.out.println("Your score is: " + playerOneScore + "-" + computerScore );
}
}
试试这个
// paper == 0
// rock == 1
// scissors == 2
System.out.println("Select 0 for Paper \nSelect 1 for Rock \nSelect 2 for Scissors");
Scanner scan = new Scanner(System.in);
int player = scan.nextInt();
Random comp = new Random();
int com = comp.nextInt(2);
System.out.println(com);
if (player == com){
System.out.println("Match is Tie");
}else{
switch (com){
case 0:
if(player == 2){
System.out.println("Player Wins the Match");
}else{
System.out.println("Comp wins the Match");
}
break;
case 1:
if(player == 0){
System.out.println("Player Wins the Match");
}else{
System.out.println("Comp wins the Match");
}
break;
case 2:
if(player == 1){
System.out.println("Player Wins the Match");
}else{
System.out.println("Comp wins the Match");
}
break;
}
}
使用:
string val = sc.nextLine().toLower();
然后:
if(val.equals("rock") {
userPlay = ROCK;
}
else if(...) {
//..
}
您可以使用嵌套的if循环,如下所示:
if(userPlay == ROCK) {
if(computerPlay == ROCK) {
System.out.println("The computer chose the same thing you did! \n Tie!");
}
else if(computerPlay == PAPER) {
System.out.println("The computer chose paper and you chose rock. \n You lose!");
computerScore++;
}
else {
System.out.println("The computer chose scissors and you chose rock. \n You win!");
playerOneScore++;
}
}
else if(userPlay == PAPER) {
if(computerPlay == ROCK) {
System.out.println("You computer chose rock, you chose paper.\n You win!");
playerOneScore++;
}
else if(computerPlay == PAPER) {
System.out.println("The computer chose the same thing you did! \n Tie!");
}
else {
System.out.println("The computer chose scissors and you chose paper. \n You lose!");
computerScore++;
}
}
//I think you get the idea...
我想让用户可以输入“岩石”或“纸”,而不是1,2和3
使用key.nextLine().toLower()
,然后测试此值是否等于“rock”等。
我应该使用嵌套的if-else语句
代码中的注意事项:
if (computerPlay ==SCISSOR && userPlay==ROCK ){
// etc.
}
if (computerPlay ==SCISSOR && userPlay==PAPER ){
// etc.
}
您检查了两次computerPlay==SCISSOR。而使用嵌套语句,您可以执行以下操作:
if (computerPlay == SCISSOR) {
if (userPlay == ROCK) {
// etc.
else if (userPlay == PAPER) {
// etc.
}
}
我的任务是编写一个程序,让用户对着电脑玩石头、纸、剪刀的游戏。 说明: 主方法应该有两个嵌套的循环,其中外循环将允许用户根据需要经常玩游戏,内循环将玩游戏,只要有一个平局。在userChoice()方法的while循环中调用方法isValidChoice()来验证用户输入的选项必须是“Rock”、“Paper”或“Scissors”。如果输入了无效的字符串,isValidChoice()将返回fa
本文向大家介绍Java实现石头剪刀布小游戏,包括了Java实现石头剪刀布小游戏的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Java实现石头剪刀布的具体代码,供大家参考,具体内容如下 代码: 运行效果: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
我正在为我的Java入门课做一个“石头,纸,剪刀”游戏。这里是提示:创建一个“石头,纸,剪刀”的游戏,在这里计算机随机选择石头,纸,或剪刀。让用户输入一个1、2或3的数字,每个数字代表三种选择中的一种。确定胜利者。游戏应该要求用户重新玩,如果是继续玩,如果不是停止玩。一旦用户停止播放,程序应该打印总的胜利数。 我在正确的位置声明我的变量时遇到了问题,因为我试图使用一个方法,这样我就可以调用它来再次
本文向大家介绍Java实现的剪刀石头布游戏示例,包括了Java实现的剪刀石头布游戏示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Java实现的剪刀石头布游戏。分享给大家供大家参考,具体如下: ChoiceAnswer.java Game.java 运行结果: 更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《
本文向大家介绍Python实现石头剪刀布游戏,包括了Python实现石头剪刀布游戏的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了一个简单的小游戏,分享给大家。 利用随机函数制作石头剪刀布小游戏 程序只运行一次 每次出拳一次要运行一次,很麻烦,要让程序一直运行 在程序中加个while条件就解决啦 长期玩游戏不利于身心健康,玩游戏要有度 那就默认游戏一开始有三次机会吧,每玩一次减掉一次
我写了一个“石头、布、剪刀”游戏: 它可以工作,但是,这是很多s,我知道这对于语句时,问题是我不知道如何。 我试着根据输入使用一个返回语句:“返回0代表石头,1代表布,2代表剪刀”,然后使用一个条件来表示“嘿,如果一个玩家返回1,另一个玩家也返回1,那么就把‘平局’放进去,其他可能的结果也一样。”。 我试图将一个数字与结果联系起来: