【问题标题】:Main method not executing certain methods主要方法不执行某些方法
【发布时间】: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()肯定执行你告诉它执行的内容。还有其他一些错误会导致程序的输出不是您所期望的。
  • 你永远不会打电话给compWinuserWin,而且你比较字符串错误并且有2 个answer 变量——其中一个没有被使用
  • 不要使用&amp; 和,使用&amp;&amp;
  • 其实“and”根本不需要存在。

标签: java methods main


【解决方案1】:

正在调用您的最终方法decideWinner,但是该方法中的代码错误且不完整。首先在比较字符串时你应该使用if(compChoice.equals(answer)) 其次它只涵盖了平局场景你仍然需要调用compWin和userWin方法所以你需要添加一些其他的ifs。

** 更新** 冒昧地清理一下您的代码:

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();
        showCompAnswer();
        decideWinner();
    }
    public static void getAnswer(){
        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();
        if(compAnswer > .66){
            compChoice = "rock";
        } else if(compAnswer > .33) {
            compChoice = "paper";
        } else{
            compChoice = "scissors";
        }
        System.out.println(compChoice);
    }
    public static void decideWinner(){
        if(compChoice.equals(answer)){
            tie();
        } else if ((compChoice.equalsIgnoreCase("rock") && answer.equals("scissors")) ||
                (compChoice.equalsIgnoreCase("paper") && answer.equals("rock")) ||
                (compChoice.equalsIgnoreCase("scissors") && answer.equals("paper"))) {
            compWin();
        } else {
            userWin();
        }

    }
    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!");
    }
}

【讨论】:

  • 谢谢,我不知道命令是否存在,但我正在学习。
  • 不用担心。并且一定要阅读别人留下的cmets,它们是有效的。特别是 & 与 && 因为这些运算符做了两件非常不同的事情。
【解决方案2】:

你不能使用==比较字符串尝试使用equals(String)方法。

你的参数传递不正确,getAnswer(String answer)方法不需要使用字符串参数。不带参数写。

如下更改您的代码:

public static void getAnswer() {
    System.out.println("Welcome to Rock, Paper, Scissors Vs. the computer.\nWhich do you     choose:");
    answer = Andrew.nextLine();
}

public static void decideWinner() {
    if (compChoice.equals(answer)) {
        tie();
        System.out.println("Hi");
    }
}

阅读这个与字符串比较相关的线程。 How do I compare strings in Java?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多