2017-05-05 18 views
-2

旅行者每天都會跑幾公里。其任務是創建一種方法,顯示他從旅程開始的路徑(它看起來如下:「第一天: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公里。 如何寫這個程序沒有開關和案例。 非常感謝提前

+2

是否有任何一種公式如何計算距離? – XtremeBaumer

+3

看起來像隨機距離。所以你可能想要使用一個發電機來獲得隨機距離並添加它們? –

+0

我想你會在掌握如何計算距離時自己回答這個問題。在我看來,這並不是一個編程懷疑,但你顯然不瞭解你手上的功能問題! – sergiol

回答

0

如果我們不知道你如何得到這些數字,我們不能真正幫助,但你可以做的是使用地圖。

假設你的男人每天可以步行到50公里。你可以使用:

public static void main(String[] args) { 
    Map<Integer, Integer> walks = new HashMap<Integer, Integer>(); 

    int nbDays = 20; //number of days he'll walk 
    int maxKmPerDay = 50; 


    // This fills up your map with distances and days 
    Random r = new Random(); 
    for (int i = 0; i < nbDays; i++) { 
     walks.put(i, r.nextInt(maxKmPerDay)); 
    } 


    for (int i = 0; i < nbDays; i++) { 
     System.out.println("Distance = " + walks.get(i) + " km."); 
    } 
} 

如果你沒有保存價值或與他們做計算,你可以跳過地圖部分,並直接打印:

System.out.println("Distance = " + r.nextInt(50) + " km."); 
+0

謝謝你的幫助非常 – Vit

+0

@Vit沒問題,這是該網站:) 如果回答您的問題,可隨時將其標記爲接受答案的目的,所以這個問題被關閉。 – Nathan

0

一個可能溶液(如果你希望他一遍又一遍地重複相同的距離)(請原諒我在這裏使用C#,因爲我並不坐在Java IDE的前面; Java應該非常相似):

private static void Distances() 
    { 
     Console.WriteLine("Enter the number of the day:"); 
     int day; 

     // Keep prompting the user until they enter a valid integer 
     while (!int.TryParse(Console.ReadLine(), out day)) 
     { 
      Console.WriteLine("Please enter a valid integer"); 
     } 

     String distance = null; 

     // Use modular arithmetic to determine 
     int multipleOf5 = day % 5; 

     switch (multipleOf5) 
     { 
      // Even multiple of 5 
      case 0: 
       distance = "9 km"; 
       break; 
      // E.g.: day 6 should be 10 km again 
      case 1: 
       distance = "10 km"; 
       break; 
      case 2: 
       distance = "7 km"; 
       break; 
      case 3: 
       distance = "13 km"; 
       break; 
      case 4: 
       distance = "11 km"; 
       break; 
     } 
     Console.WriteLine(distance); 
    } 

在這種情況下,例如,第6天將具有與第1天相同的距離(例如,第11天)。同樣,第7天就會有相同的距離,每日2次。

如果真的是完全隨機的,你不關心它是關於後續調用一致的,你只需要每次都產生一些隨機數 - 你不甚至不關心用戶的輸入是什麼。無論如何,這只是一個隨機數字。

Random r = new Random(); 
// He walks some distance from, for example, 5 km to 30 km 
int distance = r.Next(5, 30); 
Console.WriteLine(distance + " km"); 

如果你想這樣會變得稍微複雜一些。在這種情況下,你想要的結果存儲在一個哈希表:

// Build a hash table 
    private static Dictionary<int, int> dict = new Dictionary<int, int>(); 

    // In C#, you must use the same instance of Random across all callsdue to the fact that 
    // the pseudorandom number generator is seeded by time 
    private static void DistanceCached(Random r) 
    { 
     Console.WriteLine("Enter the number of the day:"); 
     int day; 

     // Keep prompting the user until they enter a valid integer 
     while (!int.TryParse(Console.ReadLine(), out day)) 
     { 
      Console.WriteLine("Please enter a valid integer"); 
     } 

     // Generate a value for this day if we haven't yet 
     if (!dict.ContainsKey(day)) 
     { 
      dict[day] = r.Next(5, 30); 
     } 

     Console.WriteLine(dict[day] + " km"); 
    } 

如果你想獲得真的複雜,你可以序列這和反序列化這一所以它的整個程序的多個電話一致。

相關問題