+ 2

I don't know why I am Getting error in this? Error is " you must declare main class" and the variable is not defined.

Kindly tell what is wrong in this??? import java.util.Scanner; class Ans{ public static void main(String[] args){ Scanner s = new Scanner(System.in); int sum = 0; for(int i = 0; i<10;i++){ System.out.println("Enter a number"); int x= s.nextInt(); sum = sum+x; } System.out.println("Sum is "+sum); } }

31st Jul 2020, 8:52 AM
Mallika Gupta
8 Respostas
+ 1
Did you give 10 numbers as input, each in different lines? I tried to run it with 10 numbers and it worked.
31st Jul 2020, 9:02 AM
Ipang
+ 1
I have used the loop na then why should I input th seperately? Also nothing happened even when I wrote public before class🥺🥺
31st Jul 2020, 9:22 AM
Mallika Gupta
+ 1
Ya I have seen the link...but my code is somewhat write na bcz instead of 10 if it was to input 100 integers then surely we would have used loop only na
31st Jul 2020, 6:42 PM
Mallika Gupta
0
~ swim ~ Will there be a problem as the class 'Ans' isn't specified public?
31st Jul 2020, 9:19 AM
Ipang
0
Then what is the correct code? Can anyone help
31st Jul 2020, 5:37 PM
Mallika Gupta