//varible int time won't display the numeric value entered
聲音 的速度下表顯示了聲音在空氣,水和鋼的近似速度: 中速 空氣1100每秒 英尺水4,900英尺每秒 鋼16,400英尺每秒 編寫一個程序,要求用戶輸入「空氣」,「水」或「鋼」,以及聲波在介質中傳播的距離。然後該程序應顯示所需時間的數量。您可以通過以下公式計算出在空中傳播聲音所需的時間: 時間5距離/ 1,100 您可以使用以下公式計算聲音在水中行走的時間量: 時間5距離/ 4900 可以計算所花費的時間的聲音在鋼鐵旅行用下面的公式量: 時間5距離/ 16400/varible INT時間將不顯示的數字值輸入
import java.util.Scanner;
public class ProgramSpeedOfSound{
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
String input;
System.out.println("Enter Air, water or steel ");
input = keyboard.nextLine().toUpperCase();
if(input.equals("Air")){
System.out.println("what is the Distance? ");
int Distance = keyboard.nextInt();
int var = 1100;
double time = Distance/var;
System.out.println("it would take " + time);
}
else if(input.equals("Water")){
System.out.println("what is the Distance? ");
int Distance = keyboard.nextInt();
double time = (((Distance/ 4900)));
System.out.println("it would take " + time);
}
else{
System.out.println("what is the Distance? ");
int Distance = keyboard.nextInt();
double time = Distance/ 16400;
System.out.println("it would take " + time);
}
}
}
確保您比較兩個字符串也全部大寫。例如。將'Air'改爲'AIR'和'Water'改爲'WATER' – 4castle