这是一个调用方法x次数的while循环。
while (count<=rounds){
System.out.println("Round " + round);
PlayerVsPlayer.results();
count++;
round++;
}
PlayerVsPlayer.results()询问用户输入并调用另一个语句。
Scanner scan = new Scanner(System.in);
System.out.print("Choose (R)ock, (P)aper, or (S)cissors:");
String Player1 = scan.nextLine();
System.out.print("Choose (R)ock, (P)aper, or (S)cissors:");
String Player2= scan.nextLine();
String[] Results = compare.winScore(Player1, Player2);
这是compare.winScore(Player1,Player2)中if else语句的片段:
static String[] winScore(String x, String y){
if (x.equals(y)) {
winnerScoreRule.add("Round results is a tie, no one wins.");
winnerScoreRule.add("");
winnerScoreRule.add("");
}
else if ((x.equals("Rock")) && (y.equals("Paper"))){
score2++; count++;
winnerScoreRule.add("Player 2 wins this round!");
winnerScoreRule.add("The score is " + score1 +" to "+ score2);
winnerScoreRule.add("Paper beats rock!"); // etc.
}
我遇到的问题是if else语句第一次执行时,它完美地运行。第二次,if else语句跳过并返回第一次调用的值。有谁知道为什么会这样?谢谢你的帮助!
答案 0 :(得分:0)
我重写了这个winScore方法来检查&#34;其他&#34;例。
static String[] winScore(String x, String y){
if (x.equals(y)) {
winnerScoreRule.add("Round results is a tie, no one wins.");
winnerScoreRule.add("");
winnerScoreRule.add("");
}
//you're asking them to enter (R), (P) or (S)
else if ((x.toLowerCase().equals("r")) && (y.toLowerCase().equals("p"))){
score2++; count++;
winnerScoreRule.add("Player 2 wins this round!");
winnerScoreRule.add("The score is " + score1 +" to "+ score2);
winnerScoreRule.add("Paper beats rock!"); // etc.
}
....//other logic for Rock Paper Scissors
else
{
throw WrongOptionSelectedException(); //custom exception for wrong entry
}