【发布时间】:2014-08-23 22:26:09
【问题描述】:
我为石头剪刀布游戏编写的一些代码有问题。在我的主要方法中,我有另一种方法来决定获胜者,但它没有执行该方法。它表现得好像它甚至不存在一样。对不起,如果有一个明显的答案,我只是一个初学者。例如,如果它是平局,则不会在 decisionWinner() 方法中执行 tie 方法。感谢您的帮助
import java.util.Scanner;
import java.util.Random;
public class appels {
public static Scanner Andrew = new Scanner(System.in);
public static Random Hello = new Random();
public static double compAnswer;
public static String compChoice;
public static String answer;
public static void main(String [] args){
getAnswer(answer);
showCompAnswer();
decideWinner();
}
public static void getAnswer(String answer){
System.out.println("Welcome to Rock, Paper, Scissors Vs. the computer.\nWhich do you choose:");
answer = Andrew.nextLine();
}
public static void showCompAnswer(){
compAnswer = Hello.nextDouble();
//System.out.println(compAnswer);
if(compAnswer > .66){
compChoice = "rock";
}else if(compAnswer>.33 & compAnswer<.66){
compChoice = "paper";
}else{
compChoice = "scissors";
}
System.out.println(compChoice);
}
public static void decideWinner(){
if(compChoice == answer){
tie();
System.out.println("Hi");
}
}
public static void tie(){
System.out.println("It's a tie.");
}
public static void compWin(){
System.out.println("The computer wins. :(");
}
public static void userWin(){
System.out.println("You win!");
}
}
【问题讨论】:
-
不过,这不是唯一的问题......我不确定我是否应该以欺骗或拼写错误的方式关闭,因为 1) 除了最后的
tie()和 2) 参数之外,你永远不会调用任何东西任务,除其他外。main()肯定执行你告诉它执行的内容。还有其他一些错误会导致程序的输出不是您所期望的。 -
你永远不会打电话给
compWin或userWin,而且你比较字符串错误并且有2 个answer变量——其中一个没有被使用 -
不要使用
&和,使用&&。 -
其实“and”根本不需要存在。