英文:
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()
中,您传入a
和b
,然后根据返回的结果打印“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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论