JAVA 2. (8 points) Write a code segment which prompts the user to enter the pric
ID: 3601680 • Letter: J
Question
JAVA 2. (8 points) Write a code segment which prompts the user to enter the price of a tour and receives input from the user. A valid tour price is between $52.95 and $259.95, inclusive. Your program should continue to repeat until a valid tour price has been entered. If the user enters an invalid price, display a message describing why the input is invalid. You may assume that the user only enters a real number. The user will not enter any invalid characters. Although the use of constants to test the minimum and maximum price of a tour would be appropriate if this were part of a program, it is not necessary to take time declaring constants for the purposes of this exam.
Explanation / Answer
Please find my code.
import java.util.Scanner;
public class JavaProg {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final double MIN = 52.95;
final double MAX = 259.95;
double input;
while(true) {
System.out.print("Enter a number in range inclusove ("+MIN+","+MAX+"): ");
input = sc.nextDouble();
if(input < MIN) {
System.out.println("You have enterd less than "+MIN);
}else if(input > MAX){
System.out.println("You have enterd greater than "+MIN);
}else{
break;
}
}
sc.close();
System.out.println("Your Input: "+input);
}
}
/*
Sample run:
Enter a number in range inclusove (52.95,259.95): 23
You have enterd less than 52.95
Enter a number in range inclusove (52.95,259.95): 67
Your Input: 67.0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.