This program will prompt the user to enter a medium either air, water, and steel & a distance .Then calculate the distance a sound wave will travel will travel through the medium.
So, I wrote the whole program but I didnt read the last bit that my professor added to the homework which is the following paragraph. Now Im stuck because Im not quite sure how to ADD this to my program. I was using if statements maybe I can add it in one?
The program prompts for the medium with: "Enter one of the following: air, water, or steel:" and reads the medium. If the medium is not air, water, or steel the program prints the message: "Sorry, you must enter air, water, or steel" and nothing else. Otherwise the program prompts for the following distance input.
I tried a while loop and adding another if statement but my problem is the syntax really. Because ive never had to command the user to type in specific strings.
import java.util.Scanner;
public class SpeedOfSound
{
public static void main(String[ ] args)
{
double distance;
double time;
Scanner keyboard = new Scanner(System.in);
//prompt the user to enter the medium through which sound will
travel through
System.out.print("Enter one of the following: air, water, or
steel:");
String input;
input = keyboard.nextLine();
//prompt the user to enter a distance
System.out.print("Enter distance in feet: ");
distance = keyboard.nextDouble();
//determine if medium is air, water, steele and calculate
if (input.equals("air"))
{
time = (distance / 1100);
System.out.println("The total time traveled is " + time + "
feet per second.");
}
else if (input.equals("water"))
{
time = (distance / 4900);
System.out.println("The total time traveled is " + time + "
feet per second.");
}
else if (input.equals("steel"))
{
time = (distance / 16400);
System.out.println("The total time traveled is " + time + "
feet per second.");
}
}
}
My expected result is to make the user only type in either Air, water, or Steel.
Aucun commentaire:
Enregistrer un commentaire