Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 你能做一个方法==另一个方法吗?_Java_If Statement_Methods_Equals - Fatal编程技术网

Java 你能做一个方法==另一个方法吗?

Java 你能做一个方法==另一个方法吗?,java,if-statement,methods,equals,Java,If Statement,Methods,Equals,我评论了代码不起作用的地方。为什么不呢?我确保calculate()是=到computerInput(),但它会打印出“这不起作用”!!为什么呢 import java.util.Scanner; class Drank { private static String[] userOption = new String[] { "Rock", "Paper", "Scissors" }; private static String[] computerOption = new

我评论了代码不起作用的地方。为什么不呢?我确保
calculate()
=
computerInput()
,但它会打印出“这不起作用”!!为什么呢

import java.util.Scanner;

class Drank {

    private static String[] userOption = new String[] { "Rock", "Paper", "Scissors" };
    private static String[] computerOption = new String[] { "Rock", "Paper", "Scissors" };

    public static void main(String[] args) {
        System.out.println("Enter: Rock, Paper Or Scissors");
        System.out.println(calculate());
        System.out.println(computerInput());
        result();
    }

    public static int calculate() {
        Scanner input = new Scanner(System.in);
        String userInput = input.nextLine();

        int calculate = 0;
        if (userInput.equals(userOption[0])) {
            calculate = 0;
        } else if (userInput.equals(userOption[1])) {
            calculate = 1;
        } else if (userInput.equals(userOption[2])) {
            calculate = 2;
        }
        return calculate;
    }

    public static int computerInput() {
        int ai = (int) (Math.random() * 3);
        return ai;
    }

    public static void result() {
            if (calculate() == computerInput()) {
            System.out.println("Computer's Pick:" + computerOption[computerInput()]);
        } else {
            // THIS IS WHERE THE PROBLEM
            // IS! It always prints out
            // the'else' even when both
            // are equal?
            System.out.println("This doesnt work"); 
        }
    }
}

您再次调用这些方法,而不是使用重用它们的返回值

int calc = calculate();
int comp = computerInput();

System.out.println(calc);
System.out.println(comp);

result(calc, comp); // Reuse them here!
当然,这需要向
result
方法添加额外的参数

public static void result(int calc, int comp) // Add params here
{
    if(calc == comp) // Compare them here!
    {
        System.out.println("Computer's Pick:" + computerOption[computerInput()] );
    } else
    {
        System.out.println("This doesnt work");
    }
}


当您两次调用这些方法时,将生成两次随机数,并询问两次用户输入。

我在代码中看不出为什么比较不起作用

请注意,每次调用computerInput时,它都会返回不同的值,因此它打印的值与比较中使用的值不同


您需要将computerInput的结果存储在某个位置,以便在两个位置显示相同的数字。

您的代码在
computerInput()
中使用随机数生成器。每次运行该方法时,该值都会更改。因此,你必须幸运地猜出计算机的答案。但你显然猜错了


您正在再次调用
computerInput()
,这意味着即使您猜到了正确的值,显示的结果也可能不同。

您可能应该尝试以下方法:

public static void result(){

int calculate = calculate();
int computerInput = computerInput();

    if(calculate == computerInput){
        System.out.println("Computer's Pick:" + computerOption[computerInput] );
    } else {
        System.out.println("This doesnt work"); 
    }

}
我做了一些小改动,(不要调用方法两次)


您确定从这些方法中得到了正确的结果吗?“我确保calculate()为==to computerInput()。当一种方法是随机的时,这怎么可能呢
  main(){
      int c = calculate();
      int ci=computerInput();
    }

public static void result(int c, int ci){

    if(c==ci){
        System.out.println("Computer's Pick:" + computerOption[computerInput()] );
    } else {
        System.out.println("This doesnt work"); //THIS IS WHERE THE PROBLEM IS! It always prints out the'else' even when both are equal?
    }