主方法不执行某些方法



我在编写剪刀布石头游戏时遇到了一个问题。在我的主方法中,我有另一个方法决定获胜者它没有执行那个方法。它表现得好像根本不存在一样。抱歉,如果有一个明显的答案,我只是一个初学者。例如,如果它是一个领带,它将不会在decisiwinner()方法中执行领带方法。谢谢你的帮助

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

您的最终方法decideWinner正在被调用,但是该方法内部的代码是坏的和不完整的。首先,当比较字符串时,你应该使用if(compChoice.equals(answer)),其次,这只涵盖了平局情况,你仍然需要调用compWin和userWin方法,所以你需要添加一些其他的if。

** Update **冒昧地整理了一下代码:

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

您不能使用==比较字符串,尝试使用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");
    }
}

阅读这个与字符串比较相关的线程。如何比较Java中的字符串?

相关内容

  • 没有找到相关文章

最新更新