我正在为我的Java入门课做一个“石头,纸,剪刀”游戏。这里是提示:创建一个“石头,纸,剪刀”的游戏,在这里计算机随机选择石头,纸,或剪刀。让用户输入一个1、2或3的数字,每个数字代表三种选择中的一种。确定胜利者。游戏应该要求用户重新玩,如果是继续玩,如果不是停止玩。一旦用户停止播放,程序应该打印总的胜利数。
我在正确的位置声明我的变量时遇到了问题,因为我试图使用一个方法,这样我就可以调用它来再次玩这个游戏。
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String answer = input.next();
}
public static int letsPlay()
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
while (answer.equalsIgnoreCase("yes"))
cMove = r.nextInt(3)+1;
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
while(input.hasNextInt()) {
if (userMove!=1 && userMove!=2 && userMove!=3)
{
System.out.println("Invalid move. Try again.");
System.out.println("Enter your move: ");
input.nextInt();
}
}
if(userMove==1)
{
System.out.println("You have chosen Rock!");
}
else if(userMove==2)
{
System.out.println("You have chosen Paper!");
}
else if(userMove==3)
{
System.out.println("You have chosen Scissors!");
}
if (userMove == cMove)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
rounds++;
} else if (cMove==1 && userMove==3)
{
System.out.println("Computer chose Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==1 && userMove==2)
{
System.out.println("Computer chose Rock!");
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==3)
{
System.out.println("Computer chose Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==1)
{
System.out.println("Computer chose Paper!");
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==3 && userMove==1)
{
System.out.println("Computer chose Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==3 && userMove==2)
{
System.out.println("Computer chose Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
System.out.println("Would you like to play again?");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
String yesorno = input.next();
if(yesorno.equalsIgnoreCase("yes"))
{
letsPlay();
}
else {
System.out.println ("Here are the final scores after " + rounds +" rounds:");
System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: " + tie);
}
}
}
编辑代码到目前为止,它说从我的letsPlay方法中缺少return语句:不确定如何进行…
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
String answer = input.next();
letsPlay(answer);
}
public static int letsPlay(String answer)
{
int cMove;
int userMove = 0;
int cScore = 0;
int pScore = 0;
int tie = 0;
int rounds = 0;
Random r = new Random();
Scanner input = new Scanner(System.in);
cMove = r.nextInt(3)+1;
while (answer.equalsIgnoreCase("yes"))
System.out.println("Choose your move!");
System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
userMove = input.nextInt();
while(input.hasNextInt()) {
if (userMove!=1 && userMove!=2 && userMove!=3)
{
System.out.println("Invalid move. Try again.");
System.out.println("Enter your move: ");
input.nextInt();
}
}
if(userMove==1)
{
System.out.println("You have chosen Rock!");
}
else if(userMove==2)
{
System.out.println("You have chosen Paper!");
}
else if(userMove==3)
{
System.out.println("You have chosen Scissors!");
}
if (userMove == cMove)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
rounds++;
} else if (cMove==1 && userMove==3)
{
System.out.println("Computer chose Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==1 && userMove==2)
{
System.out.println("Computer chose Rock!");
System.out.println("Paper beats Rock!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==3)
{
System.out.println("Computer chose Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==2 && userMove==1)
{
System.out.println("Computer chose Paper!");
System.out.println("Paper beats Rock!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
else if (cMove==3 && userMove==1)
{
System.out.println("Computer chose Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("Player Wins!");
pScore++;
rounds++;
}
else if (cMove==3 && userMove==2)
{
System.out.println("Computer chose Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("Computer Wins!");
cScore++;
rounds++;
}
System.out.println("Would you like to play again?");
System.out.println("Answer \"yes\" or \"no\"");
input.next();
answer = input.next();
if(answer.equalsIgnoreCase("yes"))
{
main(null);
}
else {
System.out.println ("Here are the final scores after " + rounds +"
rounds:");
System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: "
+ tie);
}
}
}
首先,在您的主方法中,一个input.next()
是额外的,没有用,所以删除它。现在在main方法中在String answer=input.next();
之后编写如下语句:
letsPlay(answer);
在letsPlay()方法中放入一个参数,如下所示:
public static void letsPlay(String answer) {
//Your code..........
//Some last edits...
Scanner input = new Scanner(System.in);
answer = input.next();
if(!(answer.equalsIgnoreCase("yes")))
{
System.out.println ("Here are the final scores after "+rounds+"
rounds:");
System.out.println("You:"+pScore+"Computer: "+cScore+"Ties: "+tie);
}
}
您没有将字符串答案传递给letsPlay()方法,这是因为letsPlay()方法不能将字符串作为参数,因为它是在没有传递参数的情况下定义的。这个问题的一个解决方案是更改方法定义以要求一个字符串变量。
public static int letsPlay()
变成
public static int letsPlay(String userInput)
然后在方法内部使用变量userInput而不是letsPLay(String userInput)方法中的字符串应答。
您遇到的下一个问题是在方法内再次调用该方法。这被称为递归,它是完全合法的,但是在这种情况下它并不理想。游戏结束后,您应该退出游戏,并在main()方法中询问用户是否愿意再玩一次。
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
do
{
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
String answer = input.nextLine();
letsPlay(answer);
}while(answer.equalsIgnoreCase("yes"));
}
我的任务是编写一个程序,让用户对着电脑玩石头、纸、剪刀的游戏。 说明: 主方法应该有两个嵌套的循环,其中外循环将允许用户根据需要经常玩游戏,内循环将玩游戏,只要有一个平局。在userChoice()方法的while循环中调用方法isValidChoice()来验证用户输入的选项必须是“Rock”、“Paper”或“Scissors”。如果输入了无效的字符串,isValidChoice()将返回fa
本文向大家介绍Java实现石头剪刀布小游戏,包括了Java实现石头剪刀布小游戏的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Java实现石头剪刀布的具体代码,供大家参考,具体内容如下 代码: 运行效果: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
我正在用java编写一个石头剪刀游戏,但有些事情我想不出来。首先,我想让用户可以输入“Rock”或“paper”,而不是1、2和3,但我想不出来。其次,我应该使用嵌套的if-else语句,但我也不知道如何使用我所做的。我的代码如下 导入Java . util . scanner;公共类RockGame {
本文向大家介绍JavaFX实现石头剪刀布小游戏,包括了JavaFX实现石头剪刀布小游戏的使用技巧和注意事项,需要的朋友参考一下 用JavaFX写一个石头剪刀布的小游戏,供大家参考,具体内容如下 课程上布置的作业,其实java很少用来写这种程序的 - GUI界面部分 - 石头剪刀布的逻辑部分 书上教的是JavaFX,其实还有很多方法,书上说AWT和Swing基本上淘汰了,但事实上… 代码 MyJav
本文向大家介绍Python实现石头剪刀布游戏,包括了Python实现石头剪刀布游戏的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了一个简单的小游戏,分享给大家。 利用随机函数制作石头剪刀布小游戏 程序只运行一次 每次出拳一次要运行一次,很麻烦,要让程序一直运行 在程序中加个while条件就解决啦 长期玩游戏不利于身心健康,玩游戏要有度 那就默认游戏一开始有三次机会吧,每玩一次减掉一次
我写了一个“石头、布、剪刀”游戏: 它可以工作,但是,这是很多s,我知道这对于语句时,问题是我不知道如何。 我试着根据输入使用一个返回语句:“返回0代表石头,1代表布,2代表剪刀”,然后使用一个条件来表示“嘿,如果一个玩家返回1,另一个玩家也返回1,那么就把‘平局’放进去,其他可能的结果也一样。”。 我试图将一个数字与结果联系起来: