如何创建一个评估主类中的两个变量的方法?

huangapple go评论62阅读模式
英文:

How can I create a method that evaluates two variables from main class?

问题

以下是您提供的代码的翻译部分:

我必须创建一个程序用户必须猜出1到10的一个数字老师要求我们创建6个方法一个用于生成随机数字另一个用于检查用户给出的答案和随机数字是否相同等等我正在编写一个方法其中输入和随机数字必须相同但我不确定是否正确

public class Guessgame {
    static int a;
    static int a1;
    static int a2;
    static int b;

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in); // 用户输入

        b = generator(); // 声明生成的随机数字在 "generator()" 中

        System.out.println("欢迎参加游戏,你需要猜出1到10的一个数字");
        System.out.println("你有三次机会");

        System.out.println(b);

        System.out.println("你的第一次猜测是什么?");

        a = input.nextInt(); // 声明用户输入的数字

        evaluator();

    }

    这是评估方法

    public static void evaluator(){
        if ( a == b ){
            System.out.println("正确");
        }
        else{
            System.out.println("错误");
        }
    }
}

只需将 "evaluator();" 更改为 "evaluator(a, b);"。

英文:

I have to create a program in which the user has to guess a number from 1 to 10, teacher asked us to create 6 methods: one to generate random numbers, other to check that the answer the user gives and the random number are the same, etc) I'm doing the method in which the input and random number has to be the same but I'm not sure I'm doing it correctly:

public class Guessgame {   
    static int a;
    static int a1;
    static int a2;
    static int b;

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in); // User input

        b = generator(); //declara el numero aleatorio generado en "generator()"

        System.out.println("Welcome to a game, you have to guess a number between 1 to 10");
        System.out.println("You have three tries");

        System.out.println(b);

        System.out.println("What is your first guess?");

        a = input.nextInt(); //declara el numero que declaro el usuario

        evaluator();   
                                 
    }

And here's the evaluator method:

        public static void evaluator(){
        if ( a == b ){
            System.out.println("Good");                
        }
        else{
            System.out.println("Bad");
        }
    }

I just had to change evaluator(); to evaluator(a, b);

答案1

得分: 1

"It's showing 'Good', because the local variables a and b that are being initialized are different from the fields (or instance variables) a and b that are being compared. Here is a good explanation of the different types of variables in Java.

In your code, int b = generator(); is creating a local variable named b. Likewise, int a = input.nextInt(); is creating a local variable named a. To fix this, remove the data type in these statements. This will change the statements from a declaration (creating a new variable) to an initialization (setting the value of a pre-existing variable).

Also, you actually cannot reference instance variables from a static method. You would need to convert the instance variables into class variables using the static modifier.

Your new code should look like this:

public class Guessgame {
static int a;
static int a1;
static int a2;
static int b;

public static void main(String[] args) {
    Scanner input = new Scanner (System.in); // User input

    b = generator(); //declara el numero aleatorio generado en 'generator()';

    System.out.println('Welcome to a game, you have to guess a number between 1 to 10');
    System.out.println('You have three tries');

    System.out.println(b);

    System.out.println('What is your first guess?');

    a = input.nextInt(); //declara el numero que declaro el usuario

    evaluator();   
}

Also, your 'evaluator()' method always prints 'Good' because it was comparing the instance variables (which were never initialized, so both of them were always 0)."

英文:

It's showing "Good", because the local variables a and b that are being initialized are different from the fields (or instance variables) a and b that are being compared. Here is a good explanation of the different types of variables in Java.

In your code, int b = generator(); is creating a local variable named b. Likewise, int a = input.nextInt(); is creating a local variable named a. To fix this, remove the data type in these statements. This will change the statements from a declaration (creating a new variable) to an initialization (setting the value of a pre-existing variable).

Also, you actually cannot reference instance variables from a static method. You would need to convert the instance variables into class variables using the static modifier.

Your new code should look like this:

public class Guessgame {   
    static int a;
    static int a1;
    static int a2;
    static int b;

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in); // User input

        b = generator(); //declara el numero aleatorio generado en "generator()"

        System.out.println("Welcome to a game, you have to guess a number between 1 to 10");
        System.out.println("You have three tries");

        System.out.println(b);

        System.out.println("What is your first guess?");

        a = input.nextInt(); //declara el numero que declaro el usuario

        evaluator();   
                                 
    }

Also, your evaluator() method always prints "Good" because it was comparing the instance variables (which were never initialized, so both of them were always 0).

答案2

得分: 0

I would convert your evaluator() method to a FUNCTION, and make it RETURN true/false via the boolean data type. Furthermore, you can make it receive the two values as PARAMETERS:

将您的evaluator()方法转换为函数,并使用boolean数据类型使其返回true/false。此外,您可以将其作为参数接收两个值:

public static boolean evaluator(int value1, int value2) {
    return value1 == value2;
}

Now, back in main(), you pass in a and b, then print either "Good" or "Bad" based on the returned results:

现在,在main()中,您传入ab,然后根据返回的结果打印“Good”或“Bad”:

if (evaluator(a, b)) {
    System.out.println("Good");                
}
else {
    System.out.println("Bad");                
}

Doing it this way also means that your a and b variables could have remained local to main(), and not changed to static members of the class.

英文:

I would convert your evaluator() method to a FUNCTION, and make it RETURN true/false via the boolean data type. Furthermore, you can make it receive the two values as PARAMETERS:

public static boolean evaluator(int value1, int value2) {
    return value1 == value2;
}

Now, back in main(), you pass in a and b, then print either "Good" or "Bad" based on the returned results:

if (evaluator(a, b)) {
    System.out.println("Good");                
}
else {
    System.out.println("Bad");                
}

Doing it this way also means that your a and b variables could have remained local to main(), and not changed to static members of the class.

huangapple
  • 本文由 发表于 2020年8月1日 05:49:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63199520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定