java*更新*现在我的程序可以编译但不运行了?
我正在编写一个程序,提示用户在电脑上玩石头剪刀。从main方法调用int-rpsls方法时遇到问题。我不确定如何从main方法调用它,因为我需要将用户从main方法输入的值输入到rpsls方法中。以下是我到目前为止所做的,是的,它充满了错误,但我主要关心的是调用该方法
更新-我又添加了一些代码,现在程序将编译。然而,当我试图运行它时,它只是冻结。有人能帮我找到一个让程序运行的解决方案吗
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;
}
}
共 (0) 个答案