我是Java和本站的新手,所以請對我犯的任何錯誤留下憐憫之心,我在我的智慧結尾!允許Java識別不等於想要的值的輸入
我想製作一個程序,通過不同的媒介來計算聲音的速度。截至目前,該程序將要求用戶輸入距離並允許輸入,對於媒體也是如此。我創建了幾個案例來計算正確工作的給定介質的答案。
問題是,當我嘗試創建一個循環來識別媒體輸入是否不是可用的4個選項之一。我能夠成功地創建一個循環,以識別距離輸入是否不是數值,並嘗試使用類似的原則來處理媒體輸入,但要麼一直處於無限循環中,要麼在我輸入時出現錯誤條目時創建的消息正確的選擇。 我已經嘗試了我曾經教過的基本循環:for,do-while等等,但我被卡住了。任何和所有的建議表示讚賞,非常感謝你!
public static void main(String[] args) {
//prompt the user about the purpose of this program
System.out.println(" The purpose of this program is to calculate the speed of sound through several mediums.\n The program user will input a distance in feet followed by a mediums and the program will output the speed in feet per second and miles per hour\n");
//declare variables
Scanner keyboard = new Scanner(System.in);
final double Air = 1126.1;
final double Water = 4603.2;
final double Steel = 20013.3;
final double Earth = 22967.4;
double OneFootPerSecond = .68181818182;
double Distance;
double AirSpeed;
double WaterSpeed;
double SteelSpeed;
double EarthSpeed;
System.out.print(" What is the distance in feet:");
//ask the user to input variables
while (!keyboard.hasNextDouble()){
System.out.println("Please enter a valid numeric value, try again: ");
keyboard.next();
}
Distance =keyboard.nextDouble();
{
System.out.print("Input the media: Air, Water, Steel, or Earth: ");
String Input = keyboard.next();
Input.toLowerCase();
switch (Input)
{
case "air":
AirSpeed = Distance/Air;
System.out.print("\n \nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through AIR" +"\n");
System.out.printf("%.6f", AirSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Air);
System.out.print(" miles per hour.");
System.out.print("\n \nEnter Yes for another calculation, else No: ");
String Another = keyboard.next();
Another.toLowerCase();
break;
case "water":
WaterSpeed = Distance/Water;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through WATER" +"\n");
System.out.printf("%.6f",WaterSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Water);
System.out.print(" miles per hour.");
break;
case "steel":
SteelSpeed = Distance/Steel;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through STEEL" +"\n");
System.out.printf("%.6f",SteelSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Steel);
System.out.print(" miles per hour.");
break;
case "earth":
EarthSpeed = Distance/Water;
System.out.print("\nThe time to for sound to travel ");
System.out.print(Distance);
System.out.print(" feet through EARTH" +"\n");
System.out.printf("%.6f",EarthSpeed);
System.out.print(" seconds or ");
System.out.printf("%.1f", OneFootPerSecond*Earth);
System.out.print(" miles per hour.");
break;
'Input.toLowerCase(); '不做任何事情,因爲它返回字符串的小寫字母。嘗試'切換(Input.toLowerCase()){'代替。 –
......或者更好,創建一個你打開的Enum來代替。這意味着你的switch語句不會被(潛在的)未來的程序員弄糊塗了。在這種情況下,這是一個糟糕的例子,因爲您正在切換用戶輸入(一個字符串),但是當您獲得Enums時,不會養成切換字符串的習慣。 :) – Izmaki
就像我下面說的,我改變了輸入建議哪些幫助,我也加入了默認,這也有幫助。但是,如何讓用戶無限次地輸入媒體,直到選擇正確的媒體爲止?編輯:如果匹配,還可以使用其中一個案例的輸入? – user2824855