程序假設要求用戶輸入水,空氣和鋼的元素。然後,他們必須輸入他們想要走多遠。我遇到的問題是我不能讓元素等於空氣,所以它可以執行公式。我知道if-else是一團糟,是我試圖弄清楚什麼可行。任何答覆讚賞。將用戶輸入與if語句的字符串進行比較
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter an element of water,air,or steel");
String element=sc.next();
System.out.println("how far do you want it to travel, dont use any unit of measurment");
double distance=sc.nextInt();
double time1=distance/1100;
double time2=distance/4900;
double time3=distance/16400;
if (element.equals(air)){
System.out.println("the time is "+time1); }
else if(element.equals.water)
System.out.println("the time is "+time2);
else if(element.equals.steel)
System.out.println(time3);
}
}
你能澄清你的問題嗎? –
我需要的是要求用戶輸入一個元素。水,空氣或鋼鐵。那麼我不得不問他們有多遠想要去那裏。然後,我會匹配他們進入空氣,水或鋼鐵的東西。所以如果他們做了空氣它會吐出「時間是」時間1 =距離/ 1100。我的麻煩是試圖讓元素變量與空氣,水或鋼鐵相匹配。 Alex Chermenin給了我一個有效的答案。 – ChunkierLizard
這是一個更好的問題,請編輯上面的問題並放入文本。謝謝! –