【发布时间】:2018-04-29 22:14:14
【问题描述】:
我想从我的 main 方法中调用 isATens 方法,但我只能在 isATens 没有参数时才能这样做。我尝试在调用者中放入相同的参数,但似乎也无法识别。
public class P1L4 {
public static void main(String[] args) {
P1L4 main = new P1L4();
main.run();
isATens(userInput); //<--- this is what I've tried doing.
}
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("Name a tens and i'll test if it's one under 100.");
int userInput = scanner.nextInt();
}
public boolean isATens(int userInput) {
System.out.println(userInput);
switch (userInput) {
case 10 : case 20 : case 30 : case 40 : case 50 : case 60: case 70: case 80: case 90 :
isUnderOneHundred(continued);
default :
System.out.println("Not under one hundred");
}
return true;
}
public boolean isUnderOneHundred(int continued) {
return true;
}
}
【问题讨论】:
-
userInput 不是变量,也不是值。尝试使用:isATens(5) 或 isATens(
)。此外,由于 isATens 不是静态方法,因此您需要通过类的实例调用它 -
你也必须调用 main.isAtens(5)。而 isUnderOneHundred 方法毫无意义。
-
在
run方法中:isAtens(userInput)在Scanner阅读之后。 -
@Stultuske userInput 怎么不是变量?不是用 int Userinput =scanner.nextInt(); 声明的吗?
-
local 在你的 run 方法中,是的,但是你尝试在你的 main 方法中使用它,那里是未知的
标签: java variables methods parameters call