求助石头剪刀布问题

Cinmay 2015-06-16 05:47:41
需要大神指点,我初学java

comment中是老师要求做的,我在取值存储到tryAgain是遇到了问题,怎么都不行。求指点

I met problem in the first comment: Store the first character of the user's response in the variable tryAgain

I used tryAgain = scan.findInLine(".").charAt(0); and got a input box. Please help me



Code:

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 = Rock\n2 = Paper\n3 = Scissors\nPlease 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 = Rock\n2 = Paper\n3 = Scissors\nPlease 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);

}

}
...全文
140 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧