n****i 发帖数: 1024 | 1 主要是第一个comment那边,输入yes or no 然后取出第一个char赋值给tryAgain,我
用了tryAgain = scan.findInLine(".").charAt(0); 跑下来是出现一个框,根本就没
有赋值给tryAgain,求大神帮忙指点下,我哪里出错了。另外我用switch没问题吧
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
private static String[] choices = { "Rock", "Paper", "Scissors" };
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
Random rGen = new Random();
String name;
int playerChoiceNum;
String playerChoice;
int computerChoiceNum;
String computerChoice;
char tryAgain;
String playerMessage = null;
System.out.print("What is your name? ");
name = scan.nextLine();
System.out.println();
System.out.print("1 = Rockn2 = Papern3 = ScissorsnPlease choose 1, 2, or
3: ");
playerChoiceNum = scan.nextInt(); scan.nextLine();
playerChoice = choices[playerChoiceNum - 1];
System.out.println();
computerChoiceNum = Math.abs(rGen.nextInt()) % 3 + 1;
computerChoice = choices[computerChoiceNum - 1];
// If the player and computer are tied OR the
// computer is beating the player, then...
// Ask the user if he is sure about his choice,
// as a yes/no
// Store the first character of the user's
// response in the variable tryAgain
// If the answer is no, then...
// Repeat the lines of code that got the
// user's choice the first time
// YOUR CODE GOES HERE
switch(computerChoiceNum - playerChoiceNum)
{
case -2:
System.out.println(name +", are you Sure you want to choose " +
playerChoice + "?" + "(Yes or No)");
break;
case 0:
System.out.println(name +", are you Sure you want to choose " +
playerChoice + "?" + "(Yes or No)");
break;
case 1:
System.out.println(name +", are you Sure you want to choose " +
playerChoice + "?" + "(Yes or No)");
break;
}
scan.nextLine();
tryAgain = scan.findInLine(".").charAt(0);
do {
System.out.print("1 = Rockn2 = Papern3 = ScissorsnPlease choose 1, 2,
or 3: ");
playerChoiceNum = scan.nextInt(); scan.nextLine();
playerChoice = choices[playerChoiceNum - 1];
System.out.println();
computerChoiceNum = Math.abs(rGen.nextInt()) % 3 + 1;
computerChoice = choices[computerChoiceNum - 1];
}
while (tryAgain =='N');
System.out.printf("%s, you chose %s.%n", name, playerChoice);
System.out.printf("The computer chose %s.%n", computerChoice);
System.out.println();
// If the player and computer made the same choice, then...
// Announce that it's a tie.
// Otherwise, based upon the player's and computer's
// respective choices, announce what beats what and
// and tell the player whether he wins or loses.
// YOUR CODE GOES HERE
switch (playerChoiceNum - computerChoiceNum)
{ case -2 :
System.out.println("Rock breaks Scissors...you win!");
break;
case -1 :
if(playerChoiceNum == 2 )
System.out.println("Scissors cut Paper...you lost!");
else
System.out.println("Paper covers Rock...you lost!");
break;
case 0 :
System.out.println(name + " you and the computer both chose " +
playerChoiceNum + ", so it's a tie.");
break;
case 1 :
if(playerChoiceNum == 3)
System.out.println("Scissors cut Paper...you win!");
else
System.out.println("Paper covers Rock...you win!");
break;
case 2 :
System.out.println("Roch breaks Scissors...you lost!");
break;
}
System.out.println(playerMessage);
}
} | b******y 发帖数: 9224 | 2 这么长的程序,要网上的人免费给回答的话,估计很难了。这种应该去找个tutor解决
。 | m******u 发帖数: 12400 | 3 没有parse输入成int,是一个很明显的error。另外,“1=papern....",每个选项之后
都有个n,这不知是个typo(你贴到这里的typo),还是你运行程序就这样的。我觉得
这也应该是个error。 | b****u 发帖数: 1130 | 4 经典的解法是用double dispatch pattern. |
|