In: Computer Science
In java please
A boolean must be used for this problem!
Directions:
* Initialize a Boolean variable "first" to true.
* Repeat (another value has been read successfully)
* If first is true
* Set the
minimum to the value.
* Set first to
false.
* Else if the value is less than
the minimum
* Set the
minimum to the value.
* Display the minimum.
So far I have:
import java.util.Scanner;
public class FindMinimumValue {
public static void main(String[] args) {
// initialize variables
Scanner console=new
Scanner(System.in);
boolean first=true;
int userNum=0, minValue=1,
userNum2=0;
//prompt
System.out.println("Please enter
two numbers. Enter 'y' when done.");
userNum=console.nextInt();
userNum2=console.nextInt();
//calculate
while (console.hasNextInt())
{
if (first==true) {
minValue=userNum;
first=false;
}
else if
(userNum<minValue){
minValue=userNum;
}
}
//display
System.out.println("The minimum is:
"+minValue);
}
}
Can you please help because the output is just coming out to minValue=1 even though 'first'=true. I need the minimum input from the user and I have to use the boolean value in my program. Thank you
Program :
import
java.util.Scanner;
public class FindMinimumValue {
public static void main(String[] args) {
// initialize variables
Scanner console=new Scanner(System.in);
boolean first=true;
int userNum=0, minValue=1,userNum2=0;
//prompt
System.out.println("Please enter two numbers. Enter 'y' when
done.");
//userNum=console.nextInt();
//userNum2=console.nextInt();
// output is just coming out to minValue=1
//even though 'first'=true
//This is because, console.hasNextInt() gives false always
//in the previous case
//This program reads 2 integers
//first number is assigned to userNum
//2nd number is assigned to userNum2(previously)
//there is no 3rd number
//therefore we get false when we use console.hasNextInt()
//please check the output of this statement
//by uncommenting above two statements userNum= and userNum2=
//System.out.println(console.hasNextInt());
//calculate
//console.hasNextInt() check whether there is an integer or
not
//on the modified case, there is two
//so this while loop execute 2 times
//And we will get correct output
while (console.hasNextInt()) {
//assigns each number entered one by one
userNum=console.nextInt();
if (first==true) {
minValue=userNum;
first=false;
}
else if (userNum<minValue){
minValue=userNum;
}
}
//display
System.out.println("The minimum is: "+minValue);
}
}
Output :