+ 1
I'm trying to write a while loop that accepts the input from the user and the test if it's positive or negative.
8 odpowiedzi
+ 1
import java.util.Scanner;
class MyClass {
public static void main(String[ ] args) {
int a;
Scanner myVar = new Scanner(System.in);
a=myVar.nextInt();
while (a>0) {
System.out.println("Your input is postive");
break;
}
}
}
+ 1
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count;
System.out.println("Enter your value:");
count = scan.nextInt();
if(count <= 0){
while (count <=0) {
System.out.println("Negative value \nEnter positive value:");
count = scan.nextInt();
}
}
System.out.println("Positive value!");
}
}
0
why with while loop, try if block
if(x<0)
cout << "negative value" << endl;
else
cout << "positive value" << endl;
where x is your input
using while
while(x<0){
cout << "negative valu";
break;}
while(x>0){
cout << "positive value";
break;
}
you can also use flag variable method. But it will make you idiot.
0
Thx but I want it with a while loop
0
I have shown example of while loop
0
You wrote if else statements I was looking for something like this, but mine not working
import java.util.Scanner;
class MyClass {
public static void main(String[ ] args) {
Scanner myVar = new Scanner(System.in);
while (myVar > 0 ) {
System.out.println("Your input is postive") ;
Else
System.out.println("put a positive number")
}
System.out.println(myVar.nextLine()); // help me fix it
}
}
0
in while loop use break after printing your value is positive,otherwise it is an infinite loop.
0
Thanks that is what I was trying to do.