0

Whats wrong with this code im trying to get a for loop and get all numbers from x(scanner) to 0

import java.util.*; import java.util.Scanner; class Dcoder { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int i; int x = sc.nextInt(); if (x < 0) { System.out.println("enter a number above 0"); } else if(x>0) { for (i=x;i>0;i=i--){ System.out.println(i); } } } }

2nd Dec 2018, 2:49 PM
Asick_Wolf
Asick_Wolf - avatar
2 Réponses
+ 15
● in 2nd for() loop, make condition i=--i; //as i=i-- , means value of i will remain i only(no decrement)(infinite loop). ● btw there was no need of writing "import java.util.Scanner" as U have already wrote "import java.util.*" (it includes Scanner Class also).
2nd Dec 2018, 2:52 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Thank you you solved it
2nd Dec 2018, 3:01 PM
Asick_Wolf
Asick_Wolf - avatar