[英]*UPDATE* now my program compiles but doesn't run?
I am writing a program that prompts the user to play rock paper scissors against the computer. 我正在编写一个程序,提示用户在计算机上玩剪刀石头布。 I'm having trouble calling the int rpsls method from the main method.
我在从main方法调用int rpsls方法时遇到麻烦。 I am not sure how to call it from the main method because I need the value of user input from the main method to be inputted into the rpsls method.
我不确定如何从main方法调用它,因为我需要将main方法中用户输入的值输入到rpsls方法中。 The following is what I have so far, and yes it's full of errors, but I am mainly concerned with calling the method.
以下是到目前为止的内容,是的,它充满了错误,但是我主要关心的是调用该方法。
UPDATE - I added some more code, and now the program will compile. 更新 -我添加了更多代码,现在程序将编译。 However, when I try to run it it just freezes.
但是,当我尝试运行它时,它只是冻结了。 Can anyone help find a solution to make the program run?
谁能帮助找到使程序运行的解决方案?
import java.util.Scanner;
public class Rpsls
{
public static void main (String [] args) {
int wins = 0;
int ties = 0;
int losses = 0;
int retVal = 0;
int output = rpsls(retVal);
//while loop
Scanner input = new Scanner (System.in);
int x = input.nextInt();
while (x > 0 && x <= 5) {
//input gesture
System.out.println(" ROCK PAPER SCISSORS LIZARD SPOCK");
System.out.println(" (1) (2) (3) (4) (5)");
System.out.print("Enter your choice, or 0 to end: ");
//call rpsls for inputted gesture
int gesture = input.nextInt();
rpsls(gesture);
//returned values: loss, win, or tie
if (output == -1 ) {
losses++;
}
else if (output == 0) {
ties++;
}
else {
wins++;
}
//count wins and losses
//end while loop
//print sum of wins and losses.
}
System.out.println("You won " + wins + " games, lost " + losses + " games, and tied " + ties + " games.");
}
public static int rpsls(int gesture) {
//generate random gesture for computer
int attack = (int)(Math.random()*5);
//decide who won
int wins = 0; int losses = 0; int ties = 0;
int retVal = 0;
if (gesture == 1 && attack == 1) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose ROCK.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 1 && attack == 2) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose PAPER.");
System.out.println("PAPER covers ROCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 1 && attack == 3) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose SCISSORS.");
System.out.println("ROCK crushes SCISSORS.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 1 && attack == 4) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose LIZARD.");
System.out.println("ROCK crushes LIZARD.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 1 && attack == 5) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose SPOCK.");
System.out.println("SPOCK vaporizes ROCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 2 && attack == 1) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose ROCK.");
System.out.println("PAPER covers ROCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 2 && attack == 2) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose PAPER.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 2 && attack == 3) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose SCISSORS.");
System.out.println("SCISSORS cut PAPER.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 2 && attack == 4) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose LIZARD.");
System.out.println("LIZARD eats PAPER.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 2 && attack == 5) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose SPOCK.");
System.out.println("PAPER disproves SPOCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 3 && attack == 1) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose ROCK.");
System.out.println("ROCK crushes SCISSORS.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 3 && attack == 2) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose PAPER.");
System.out.println("SCISSORS cut PAPER.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 3 && attack == 3) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose SCISSORS.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 3 && attack == 4) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose LIZARD.");
System.out.println("SCISSORS decapitate LIZARD.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 3 && attack == 5) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose SPOCK.");
System.out.println("SPOCK smashes SCISSORS.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 4 && attack == 1) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose ROCK.");
System.out.println("ROCK crushes LIZARD.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 4 && attack == 2) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose PAPER.");
System.out.println("LIZARD eats PAPER.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 4 && attack == 3) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose SCISSORS.");
System.out.println("SCISSORS decapitate LIZARD.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 4 && attack == 4) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose LIZARD.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 4 && attack == 5) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose SPOCK.");
System.out.println("LIZARD poisons SPOCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 5 && attack == 1) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose ROCK.");
System.out.println("SPOCK vaporizes ROCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 5 && attack == 2) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose PAPER.");
System.out.println("PAPER disproves SPOCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 5 && attack == 3) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose SCISSORS.");
System.out.println("SPOCK smashes SCISSORS.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 5 && attack == 4) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose LIZARD.");
System.out.println("LIZARD poisons SPOCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 5 && attack == 5) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose SPOCK.");
System.out.println("It's a tie!");
retVal = 0;
}
return retVal;
}
}
This line: 这行:
int rpsls = gesture(x);
...creates an int
varaible called rpsls
in your main
function, calls a method called gesture
, and passes x
into it. ...创建一个
int
称为varaible rpsls
在main
函数,调用调用方法gesture
,并通过x
进去。
You may have meant: 您可能是说:
int gesture = input.nextInt();
rpsls(gesture);
...which calls nextInt
on your Scanner
to get a number from the user, remembers it as gesture
, then calls your existing rpsls
method, passing in gesture
. ...这会在您的
Scanner
上调用nextInt
来从用户那里获取一个数字,将其记住为gesture
,然后调用您现有的rpsls
方法,并传递gesture
。
int retVal = rpsls(gesture)
; int retVal = rpsls(gesture)
; input
is inside the while block so it will not work outside that; input
在while块内部,因此它将在外部不起作用。 int rpsls(int gesture)
is poorly designed: the computer should shoose a random value for rock/paper/scissors and then compare it with the user; int rpsls(int gesture)
设计不当:计算机应为石头/纸张/剪刀选择一个随机值,然后将其与用户进行比较; Your code doesn't work, you need to at least: 您的代码无效,您至少需要:
int x = input.nextInt();
int x = input.nextInt();
after while (x > 0 && x <= 5)
statement, without this it will not use you input for game, while (x > 0 && x <= 5)
语句之后,如果没有此语句,它将不会将您的输入用于游戏, int rpsls = gesture(x);
int rpsls = gesture(x);
for for example int rpsls = rpsls(x);
int rpsls = rpsls(x);
by this, it will proceed on your input After those changes, it works fine in my opinion. 我认为这些更改之后效果很好。
EDIT: You should remove int output = rpsls(retVal);
编辑:您应该删除
int output = rpsls(retVal);
and retVal declaration from main, couse it is useless i think, and add int output = rpsls(x);
和retVal声明从主,因为我认为这没用,并添加
int output = rpsls(x);
after x = input.nextInt();
在
x = input.nextInt();
then it works properly. 然后它可以正常工作。 Also replace
int x = input.nextInt();
还替换
int x = input.nextInt();
with int gesture = input.nextInt();
用
int gesture = input.nextInt();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.