由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - rock paper scissor 求教
相关主题
请教一个多线程的问题java string stream
新手请教一个问题出个简单题,看你Java APi熟悉到什么程度
Re: 请教一个问题...Help! interface Scanner with Pdf file
问几个菜问题2低手问个问题
What does this mean?读文件时,如何才能只将空白符当成分隔符?
Eclipse不能保存UTF-8文件?新手问一个弱问题, 关于从stdin输入int或者其他数值的实现方法
java str.replaceAll("\n", "") doesn't workwhat is the problem?
java问题:如何match两个正规表达式JAVA文本文件读写问题
相关话题的讨论汇总
话题: tryagain话题: string
进入Java版参与讨论
1 (共1页)
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.
1 (共1页)
进入Java版参与讨论
相关主题
JAVA文本文件读写问题What does this mean?
请教 java 编程上的一个问题Eclipse不能保存UTF-8文件?
一个比较菜鸟的数据输入的验证问题..谢谢..java str.replaceAll("\n", "") doesn't work
编程新人求助—死循环了java问题:如何match两个正规表达式
请教一个多线程的问题java string stream
新手请教一个问题出个简单题,看你Java APi熟悉到什么程度
Re: 请教一个问题...Help! interface Scanner with Pdf file
问几个菜问题2低手问个问题
相关话题的讨论汇总
话题: tryagain话题: string