I'm trying to write a while loop that accepts the input from the user and the test if it's positive or negative. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I'm trying to write a while loop that accepts the input from the user and the test if it's positive or negative.

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 }

7th Nov 2016, 2:38 AM
darius mohammed
darius mohammed - avatar
5 Answers
+ 2
import java.util.Scanner; public class p { public static void main (String[ ]args ) { Scanner s=new Scanner (system.in); int value; System.out.println("Enter no"); value=s.nextInt(); if (value>0) { While (value>0) System.out.println("NO IS POSITIVE") } else System.out.println ("negative "); } }
13th Nov 2016, 8:44 AM
Tanushree yadav
Tanushree yadav - avatar
+ 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!"); } }
9th Nov 2016, 1:53 PM
Oleksandr Tykhonenko
Oleksandr Tykhonenko - avatar
+ 1
import java.util.Scanner; class Testcode { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int count; System.out.println("Enter your value:"); count = scan.nextInt(); while(cout>=0) { System.out.println("Positive value "); break; } if(count<0) System.out.println("negative value!"); } }
16th Nov 2016, 4:44 PM
Sonal Bansal
0
I recommend you that you ask the user for a number and Parse that input string into a number, then ask the user if they want to continue. If the answer is yes the while loop will continue but if it's no, it will break. Hint String wantToContinue = "yes"; While(wantToContinue ==" yes') {...}
7th Nov 2016, 2:47 AM
Narciso Nuñez Arias
Narciso Nuñez Arias - avatar
0
its nice guys. thanks
24th Nov 2016, 6:54 PM
Shafqat Ali
Shafqat Ali - avatar