0
Beginner question
Why can't this programme display result: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); for (int i=a-1;i<=1;i--){ int result= a*i; } System.out.println(result); } }
7 odpowiedzi
+ 3
I think you should change
the bottom limit in your for cycle. Your limit is I <=1 which means the cycle won't stop - an infinite loop.
Maybe you mean I>=1.Also the result variable should be declared outside of the for loop.
+ 2
You declared the variable result inside the for loop, so it can only be used there. To fix it, put int result before the for loop.
+ 1
I'm not great at java, but I think you should do int result = new Int ();
0
thanks
0
But still if I change the above mentioned things, it says result might not have been initialized.
import java.util.Scanner;
public class gyak {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int result;
for (int i=a-1;i>=1;i--){
result= a*i;
}
System.out.println(result);
}
}
0
That wouldn't be good syntactically, I get the error when I try to print it out.
0
I had to set the starting value of result, now it works