[英]JAVA: How do i use user inputs in a different method from the one in which it was created?
I'm making a simple program that takes 2 scores from a user, does some basic sums with he scores and provides the user with their "fitness age".我正在制作一个简单的程序,它从用户那里获取 2 个分数,对他的分数进行一些基本的求和,并为用户提供他们的“健身年龄”。 The problem is, i cannot use the data from the user as it is in different method from the calculation.
问题是,我不能使用来自用户的数据,因为它与计算的方法不同。 I have tried to copy what other code on this website looks like but i think i might have messed up my own code, any help?
我试图复制本网站上其他代码的样子,但我想我可能弄乱了自己的代码,有什么帮助吗?
import java.util.Scanner;
class Health {
public static void main(String[] args) {
int in = firstScore();
int in = secondScore();
revealAge(in);
System.exit(0);
}
public static void firstScore() {
Scanner firstScores = new Scanner(System.in);
System.out.println("What score did you recieve in your first exercise out of 40?");
int score1 = firstScores.nextInt();
return score1;
}
public static void secondScore() {
Scanner secondScores = new Scanner(System.in);
System.out.println("What score did you recieve in your second exercise out of 40?");
int score2 = secondScores.nextInt();
return score2;
}
public static void revealAge(int score1, int score2) {
int average = (score1 + score2) / 2;
int fitnessAge = (((average * 8) / 5) + 10);
System.out.println("Your average score is " + average + ".");
System.out.println("Your PC Fit age is" + fitnessAge + ".");
return;
}
}
You have several problems:你有几个问题:
firstScore()
and secondScore()
are both defined to return nothing (that's what the void
does in the method definition) - based on how you're using them, you probably want to return int
instead.firstScore()
和secondScore()
都被定义为不返回任何内容(这就是方法定义中的void
所做的) - 根据您使用它们的方式,您可能希望返回int
。in
twice in your main
method.in
你的两次main
方法。 You need to name these variables differently.int firstScore
and int secondScore
? int firstScore
和int secondScore
怎么int secondScore
?revealAge
from your main
method should pass both those parameters, not just one.main
方法调用revealAge
应该传递这两个参数,而不仅仅是一个。 Fixing these issues should help your code compile.解决这些问题应该有助于您的代码编译。 I strongly recommend stepping through some java tutorials, so that errors like this become more obvious to you in the future.
我强烈建议逐步完成一些 Java 教程,以便将来您可以更明显地看到此类错误。
You need to return int
in both secondScore()
and firstScore()
methods.您需要在
secondScore()
和firstScore()
方法中返回int
。 And you have one name( in
) for many variable declarations in your main
method.并且您的
main
方法中有许多变量声明的名称( in
)。 Made some modifications to your code as per to the problems I stated:根据我所说的问题对您的代码进行了一些修改:
import java.util.Scanner;
class Health {
public static void main(String[] args) {
int in1 = firstScore();
int in2 = secondScore();
revealAge(in1, in2);
System.exit(0);
}
public static int firstScore() {
Scanner firstScores = new Scanner(System.in);
System.out.println("What score did you recieve in your first exercise out of 40?");
int score1 = firstScores.nextInt();
return score1;
}
public static int secondScore() {
Scanner secondScores = new Scanner(System.in);
System.out.println("What score did you recieve in your second exercise out of 40?");
int score2 = secondScores.nextInt();
return score2;
}
public static void revealAge(int score1, int score2) {
int average = (score1 + score2) / 2;
int fitnessAge = (((average * 8) / 5) + 10);
System.out.println("Your average score is " + average + ".");
System.out.println("Your PC Fit age is " + fitnessAge + ".");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.