你好,我一直坚持如何让游戏继续进行,直到用户决定退出为止。我想尝试任何可能的方式让游戏结束并显示损失,关系和胜利的数量。我试图使用while循环。
import java.util.Scanner;
import java.util.Random;
public class JavaApplication15 {
public static void main(String[] args) {
int ties = 0, wins = 0, losses = 0;
String comp = "";
String user = "";
Random choice = new Random();
String startover = "y";
while (startover.equals("y")) {
int computerchoice = choice.nextInt(3) + 1;
if (computerchoice == 1)
comp = "R";
else if (computerchoice == 2)
comp = "P";
else if (computerchoice == 3)
comp = "S";
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rock (1), paper (2), or scissors (3) [no to quit]: ");
int player = scanner.nextInt();
if (player == 1) {
user = "R";
} else if (player == 2) {
user = "P";
} else if (player == 3) {
user = "S";
}
System.out.println("You choose: " + user);
System.out.println("Computer chooses: " + comp);
if (comp.equals(user)) {
System.out.println("It's a tie!");
ties++;
} else if (user.equals("R")) {
if (comp.equals("P"))
System.out.println("Computer wins! Paper beats rock!");
losses++;
} else if (comp.equals("S")) {
System.out.println("You win! Your rock beats the scissors!");
wins++;
} else if (user.equals("S")) {
if (comp.equals("P"))
System.out.println("You win! Scissor beats paper!");
wins++;
} else if (comp.equals("R")) {
System.out.println("Computer wins! Rock beats paper!");
losses++;
} else if (user.equals("P"))
if (comp.equals("R")) {
System.out.println("You win! Paper beats rock!");
wins++;
} else if (comp.equals("S")) {
System.out.println("Computer wins! Scissors beats paper!");
losses++;
}
}
{
System.out.println("Invalid input! Try again");
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no";
}
}
答案 0 :(得分:0)
您只需添加新选项4
else if (computerchoice == 4)
comp = "E";
} else if (comp.equals("E")) {
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no";
}
答案 1 :(得分:0)
而不是做scanner.nextInt();你可以做到
System.out.println("Enter rock (1), paper (2), or scissors (3) [\'n\' to quit]: ");
char selection = scanner.nextLine().charAt(0)
if(selection == '1'){ user = "R"; }
else if(selection == '2'){ user = "P"; }
else if(selection == '3'){ user = "S"; }
else if(selection == 'n'){
System.out.println(ties)
System.out.println(wins);
System.out.println(losses);
return;
}
这使得选择变量成为char,您可以测试它是1,2或3(即字符1 2还是3),还可以检查' n&#39 ;
因为您正在使用主要方法,只需将"返回"将在那时退出该计划。
答案 2 :(得分:0)
我按原样试用了你的代码,我发现你的代码永远不会停止。 while循环没有适当的退出条件。所以我做了一些小改动让它发挥作用。
int ties = 0, wins = 0, losses = 0;
String comp = "";
String user = "";
Random choice = new Random();
String startover = "y";
Scanner scanner = new Scanner(System.in); //Scanner object should not be open in loop, it is a resource wastage
while (startover.equals("y")) {
int computerchoice = choice.nextInt(3) + 1;
if (computerchoice == 1)
comp = "R";
else if (computerchoice == 2)
comp = "P";
else if (computerchoice == 3)
comp = "S";
System.out.println("Enter rock (1), paper (2), or scissors (3) [no to quit]: ");
String input = scanner.nextLine(); //take input in string
int player ;
try {
player = Integer.parseInt(input); //see if actually number was given as input
} catch(Exception ex) {
break; //if not, then we assume player wants to quit
}
if (player == 1) {
user = "R";
} else if (player == 2) {
user = "P";
} else if (player == 3) {
user = "S";
}
System.out.println("You choose: " + user);
System.out.println("Computer chooses: " + comp);
if (comp.equals(user)) {
System.out.println("It's a tie!");
ties++;
} else if (user.equals("R")) {
if (comp.equals("P"))
System.out.println("Computer wins! Paper beats rock!");
losses++;
} else if (comp.equals("S")) {
System.out.println("You win! Your rock beats the scissors!");
wins++;
} else if (user.equals("S")) {
if (comp.equals("P"))
System.out.println("You win! Scissor beats paper!");
wins++;
} else if (comp.equals("R")) {
System.out.println("Computer wins! Rock beats paper!");
losses++;
} else if (user.equals("P"))
if (comp.equals("R")) {
System.out.println("You win! Paper beats rock!");
wins++;
} else if (comp.equals("S")) {
System.out.println("Computer wins! Scissors beats paper!");
losses++;
}
}
scanner.close(); //scanner was not closed before
System.out.println("Your ties: " + ties);
System.out.println("Your wins: " + wins);
System.out.println("You losses: " + losses);
startover = "no"; //redundant actually