I’ve been given Pseudo code to translate into a Java program.
It is as follows:
-
Set a Boolean variable “first” to true.
-
While another value has been read successfully
- If first is true
- Set the minimum to the value just read
- Set first to false
- Else if the value is less than the minimum
- Set the minimum to the value
- Print the minimum
- If first is true
So far I have this:
import java.util.Scanner;
public class InputLoop
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer");
boolean first = true;
int min = 0;
int val = 0;
while (scan.hasNextInt())
{
val = scan.nextInt();
System.out.println("Enter an integer");
if (first == true)
{
min = val;
first = false;
}
else if (val < min)
{
System.out.println("Enter an integer");
min = val;
}
while (!scan.hasNextInt())
{
System.out.println (("The minimum value is") + (min));
break;
}
}
}
}
Any help would be greatly appreciated as I’m stumped!
Thanks.
You’re doing great–just keep doing what you’re doing. But here’s some stuff you’ll need to know to finish:
Let us know if something doesn’t make sense.