Question: Modify Example 2 so that the user could enter Yes or No instead of true or false, whenever he/she wants to continue or stop. Find
Modify Example 2 so that the user could enter "Yes" or "No" instead of true or false, whenever he/she wants to continue or stop. Find out on how to compare between strings. [Hint: This code: (wish == "Yes") is NOT CORRECT for this situation
You are required to create a program that will display the status/category of wind speed, based on the status/category below. Wind speed Status / Category Below 38 Windy 39 to 54 Gale Over 55 Storm The user will be prompted to enter the speed. The program allows the user to continue or stop by entering true or false. Solution : (the program uses do-while structure and boolean data type as the sentinel value to stop)
The codes:
//Lab 2 eg2: WindStatus.java import java.util.Scanner; public class WindStatus { public static void main(String [] args) { Scanner input = new Scanner(System.in); String status = ""; boolean wish; do { System.out.print("Enter the wind speed = "); double wind = input.nextDouble(); if(wind <=38) status = "Windy"; else if(wind >=39 && wind <55) status = "Gale"; else if(wind >=55) status = "Storm"; System.out.println("It is " + status); System.out.println("Do you wish to continue?[true/false]"); wish = input.nextBoolean(); } while (wish==true); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
