+ 1

What is wrong in this code? Plz help

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner mo = new Scanner(System.in); int y = mo.nextInt(); for ( y= mo.nextInt(); y<30; y++) { System.out.println(y); } } }

19th Feb 2017, 12:00 PM
sal sal
sal sal - avatar
2 Answers
+ 8
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner mo = new Scanner(System.in); int y = mo.nextInt(); for (; y<30; y++) { System.out.println(y); } } } // nextInt shouldn't be used inside for loop initialization because input has been acquired prior to the loop structure.
19th Feb 2017, 12:23 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
you are initializing the same variable(y) twice...
19th Feb 2017, 3:46 PM
Vaibhav Tandon
Vaibhav Tandon - avatar