+ 1
What is wroNG here. Is it the Order in which the Code is plAced.
import java.util.Scanner; public class Main { static void welcome() { for (int i = 0; i < num; i++) {System.out.println ("Welcome!");}} public static void main(String[] args) { Scanner read = new Scanner(System.in); int num = read.nextInt(); welcome();} } OUTPUT: CANNOT FIND SYMBOL NUM.
8 Respostas
+ 2
There is no num defined in the method welcome, neither statically in the class Main. No num, no loop. Because how long are you going to iterate if the upper limit has not been defined?
The num in method main is local to that method. The method welcome does not see num in main.
You can give num to welcome though:
static void welcome(int num) { ... }
And then call in main:
int num = read.nextInt();
welcome(num);
And also pay attention to your formatting. A neat structure is a pleasure to look at and read. For yiurself and others :)
0
But in int num=read.nextInt(), num is Already there.
0
Please reread the amendments to my previous answer :)
0
The Upper Limit Should be Covered By the Input ----- int num=read.nextInt(). Is it
0
Yes, but it is not visible in welcome. You need to pass it as argument, or make it accessible via the class (static variable).
0
import java.util.Scanner;
public class Main {
static void welcome(int num) {
for (int i = 0; i < num; i++) {System.out.println
("Welcome!");}}
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int num = read.nextInt();
welcome(num);}
}
OUTPUT:
Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Main.main(Main.java:10)
0
Are you sure you entered a number in the input box?
0
It is fine, the problem wAs I used Another editor which shows no space for Input(And not SololeArnN) . When copy paste to Solo learn, I Had the input blank Space.