主要方法不执行某些方法

时间:2014-07-02 23:44:48

标签: java methods main

我遇到了一些我为石头剪刀游戏编写的代码问题。在我的主要方法中,我有另一种方法来决定胜利者并且它没有执行该方法。它表现得好像它甚至不存在。对不起,如果有一个明显的答案,我只是一个初学者。例如,如果它是一个平局,它将不会在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!");
}
}

2 个答案:

答案 0 :(得分:0)

正在调用您的最终方法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!");
    }
}

答案 1 :(得分:0)

您无法使用==比较字符串尝试使用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?