Java: I need help writing a method called 'getEvenValue' which keeps prompting user to enter a number until they enter an even number, and then return it back. I'm trying to do this with a while-loop, using no breaks. Could you please help me? This is what I have so far; I'm supposed to edit this for practice in value-producing methods. import java.util.Scanner; public class GP4B { Scanner in = new Scanner(System.in); public int getValue() { System.out.println("Enter a number. "); int val; val = in.nextInt(); return val; } public static void main(String[] args) { GP4B c = new GP4B(); int x = c.getValue(); System.out.println("The value you entered is " + x); } }
Java: I need help writing a method called 'getEvenValue' which keeps prompting user to enter a number until they enter an even number, and then return it back. I'm trying to do this with a while-loop, using no breaks. Could you please help me? This is what I have so far; I'm supposed to edit this for practice in value-producing methods.
import java.util.Scanner;
public class GP4B
{
Scanner in = new Scanner(System.in);
public int getValue()
{
System.out.println("Enter a number. ");
int val;
val = in.nextInt();
return val;
}
public static void main(String[] args)
{
GP4B c = new GP4B();
int x = c.getValue();
System.out.println("The value you entered is " + x);
}
}
Step by step
Solved in 3 steps with 1 images