0
doubts in loop
Hi, I'm new to java...when I try to run the below program I'm getting an error..could anyone pls let me know the error. public class Program { public static void main(String[] args) { int x = 1; int y; while(x > 0) { System.out.println(x); Scanner sc=new Scanner (System.in); y=sc.nextInt(); if(x == y) { break; } x++; } } }
5 Respuestas
+ 2
You need to import the Scanner package. Put this at the top of your code:
import java.util.Scanner;
+ 2
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 1;
while (i <= n) {
System.out.println(i);
i++;
}
}
}
+ 2
so do this
if (y==10)
break;
+ 1
if I give input as 10...the loop should go on to print till 10. after that it should come out..pls help me
0
well u can try this :
import java.util.Scanner ;
public class demo
{
public static void main(String args [])
{
int x, y;
System.out.println (" Input the limit :");
y = new Scanner(System.in).nextInt();
x = 1;
while (x<=y)
{
System.out.println(x);
x++;
}
}
}
if u store 10 in variable y the program will print the increasing values of x till y (10).
OUTPUT :
Input the limit :
10
1
2
3
4
5
6
7
8
9
10
Hope u find it useful !!!