+ 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 }
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 ");
}
}
+ 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!");
}
}
+ 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!");
}
}
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') {...}
0
its nice guys. thanks