Write a Java program that takes input 3 floating-point numbers and sends output
ID: 3861990 • Letter: W
Question
Write a Java program that takes input 3 floating-point numbers and sends output the maximum and minimum value. Find the most efficient solution. The processing will take place in a sentinel controlled loop, like in the following sample output:
Sample output:
Min/Max problem for 3 floating-point numbers
============================================
Do you want to start (Y/N): y
Enter 3 float values: 1.2 2.3 3.4
You entered 1.2, 2.3, 3.4
Min = 1.2
Max = 3.4
Do you want to continue (Y/N): y
Enter 3 float values: 2.3 1.2 4.5
You entered 2.3, 1.2, 4.5
Min = 1.2
Max = 4.5
Do you want to continue (Y/N): y
Enter 3 float values: 4.5 3.4 1.2
You entered 4.5, 3.4, 1.2
Min = 1.2
Max = 4.5
Do you want to continue (Y/N): n
Explanation / Answer
This program should work but System.out.println is not proper, you can put and test:
import java.util.Scanner;
public class max_float {
public static void main(String args[])
{
System.out.println("Do you want to start (Y/N)");
Scanner in=new Scanner(System.in);
String ans=in.nextLine();
while(ans=="Y")
{
System.out.println("Enter the 3 float values:");
float max=0;
float min=0;
float num1=in.nextFloat();
float num2=in.nextFloat();
float num3=in.nextFloat();
if(num1>num2)
{
if(num1>num3)
{
max=num1;
}
else max=num3;
}
else
{
if(num2>num3)
max=num2;
else max=num3;
}
if(num1<num2)
{
if(num1<num3)
min=num1;
else
min=num3;
}
else
{
if(num2<num1)
{
if(num2<num3)
min=num2;
else
min=num3;
}
}
System.out.println("Max: "+max);
System.out.println("Min: "+min);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.