I would like to call the isATens method from my main method but im only able to do that when isATens has no parameter. I'v tried putting the same parameter in the caller, but that does't seem to recognize that either.
我想从我的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;
}
}
1 个解决方案
#1
1
There are some Java concepts that you apparently haven't learned yet: Scope and Instance vs. static methods. Read the appropriate chapters of your Java textbook if you have difficulties understanding my following comments.
您似乎还没有学到一些Java概念:范围和实例与静态方法。如果您在理解我的以下注释时遇到困难,请阅读Java教科书的相应章节。
int userInput = scanner.nextInt();
is declared inside the scope of the run()
method, and therefore not visible in the main()
method. If you want to see userInput
outside of the run() method, I'd make it the return value of that method:
int userInput = scanner.nextInt();在run()方法的范围内声明,因此在main()方法中不可见。如果你想在run()方法之外看到userInput,我会把它作为该方法的返回值:
public int run() {
...
int userInput = scanner.nextInt();
return userInput;
}
You're mixing instance and static methods without any visible concept when to use which kind. When you want to call an instance method from a static one, you need to name the instance before the dot, so at least it has to be main.isATens(userInput);
instead of isATens(userInput);
(after you've solved the userInput
issue).
您在使用哪种类型时混合实例和静态方法而没有任何可见的概念。如果要从静态方法调用实例方法,则需要在点之前命名实例,因此至少它必须是main.isATens(userInput);而不是isATens(userInput); (在您解决了userInput问题之后)。
Your program logic is strange, e.g. I'd expect a method like isUnderOneHundred(int continued)
to return true if the parameter is under 100, but that method doesn't even have any look at its parameter and returns true for any number you pass in.
您的程序逻辑很奇怪,例如我希望像isUnderOneHundred(int continue)这样的方法在参数小于100时返回true,但是该方法甚至没有查看其参数,并且对于传入的任何数字都返回true。
#1
1
There are some Java concepts that you apparently haven't learned yet: Scope and Instance vs. static methods. Read the appropriate chapters of your Java textbook if you have difficulties understanding my following comments.
您似乎还没有学到一些Java概念:范围和实例与静态方法。如果您在理解我的以下注释时遇到困难,请阅读Java教科书的相应章节。
int userInput = scanner.nextInt();
is declared inside the scope of the run()
method, and therefore not visible in the main()
method. If you want to see userInput
outside of the run() method, I'd make it the return value of that method:
int userInput = scanner.nextInt();在run()方法的范围内声明,因此在main()方法中不可见。如果你想在run()方法之外看到userInput,我会把它作为该方法的返回值:
public int run() {
...
int userInput = scanner.nextInt();
return userInput;
}
You're mixing instance and static methods without any visible concept when to use which kind. When you want to call an instance method from a static one, you need to name the instance before the dot, so at least it has to be main.isATens(userInput);
instead of isATens(userInput);
(after you've solved the userInput
issue).
您在使用哪种类型时混合实例和静态方法而没有任何可见的概念。如果要从静态方法调用实例方法,则需要在点之前命名实例,因此至少它必须是main.isATens(userInput);而不是isATens(userInput); (在您解决了userInput问题之后)。
Your program logic is strange, e.g. I'd expect a method like isUnderOneHundred(int continued)
to return true if the parameter is under 100, but that method doesn't even have any look at its parameter and returns true for any number you pass in.
您的程序逻辑很奇怪,例如我希望像isUnderOneHundred(int continue)这样的方法在参数小于100时返回true,但是该方法甚至没有查看其参数,并且对于传入的任何数字都返回true。