旅行者每天都會跑幾公里。其任務是創建一種方法,顯示他從旅程開始的路徑(它看起來如下:「第一天:10公里;第二天:7公里;第三天:13公里;」)。該方法應適用於任何天數的旅行。如何寫日間和公里
我寫的followiong代碼,但它僅適用5天: `
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter the number of the day:\n");
int day = in.nextInt();
String distance = null;
switch(day){
case 1:
distance = "10 km";
break;
case 2:
distance = "7 km";
break;
case 3:
distance = "13 km";
break;
case 4:
distance = "11 km";
break;
case 5:
System.out.println("distance ="+9+ "km");
break;
default:
for(int i =0; i>distance.charAt(i); i++){
System.out.println("distance ="+i);
}
break;
}
System.out.println(distance);
}
` 你會這麼好心來幫我寫代碼differend天。例如,在第17天他去了8公里。 如何寫這個程序沒有開關和案例。 非常感謝提前
是否有任何一種公式如何計算距離? – XtremeBaumer
看起來像隨機距離。所以你可能想要使用一個發電機來獲得隨機距離並添加它們? –
我想你會在掌握如何計算距離時自己回答這個問題。在我看來,這並不是一個編程懷疑,但你顯然不瞭解你手上的功能問題! – sergiol