簡體   English   中英

我試圖以小時和分鍾(00:00)的形式在我的代碼中輸出時間,並且需要幫助。 這就是我到目前為止

[英]I am trying to output the times in my code in hours and mins (00:00) and need help. This is what i have so far

我有我的代碼,但是在解決方案3中,我希望將以分鍾為單位的時間轉換為小時和分鍾(即,將341分鍾轉換為5小時41分鍾)。

  public class CityToSurf
  {
        //Method for lowest time index
        private int getMinIndex(int[] times){
        int minValue = times[0]; 
        int minIndex = 0;//default value to start with
        for(int counter=0;counter<times.length;counter++){  
            if(times[counter] < minValue){ 
              minIndex = counter;
              minValue = times[counter]; 
            }  
           }   
           return minIndex; 
        }
        public static void main (String[] arguments){

        CityToSurf cs = new CityToSurf();

        //create array of times and names
        int[] times ={341,273,278,329,445,402,388,275,243,334,412,393,299,343,317,265};
        String[] names ={"Elana","Thomas","Hamilton", "Suzie", "Phil","Matt", "Alex", "Emma", "John","James" ,"Jane", "Emily", "Daniel" ,"Neda", "Aaron", "Kate"};

        //Solution 1      
        int fastestRunnerIndex =  cs.getMinIndex(times);
        System.out.println("Solution 1:");
        System.out.println(names[fastestRunnerIndex]+ "  "+ times[fastestRunnerIndex] );

        //Solution 2
        System.out.println("Solution 2:");
        cs.getSortedArray(times, names);

      //Solution 3
        System.out.println("Solution 3:");
        cs.timeConvert(times, names);


    }

    private void getSortedArray(int[] times, String[] names){

         //store in a local variable the unchanged/old list
         int[] oldTimes = new  int[times.length];

         for (int i=0;i<times.length;i++){
              oldTimes[i] = times[i];

         }

         java.util.Arrays.sort(times);//sort the times from min to max times for the PLACEMENT

         int place =1; //this is to initialize the placement for a list from 1 to 16.

        //loop over my old times list and match the time in the old times with sorted times to find the corresponding index that will be used to get name and time
         for (int i=0;i<times.length;i++){
             //SORTED the min to max time from the list 
             int newSortedTime= times[i];


             //loop over the sorted times to match the time
             int oldIndex =0;
             for (int j=0; j<oldTimes.length;j++){
                 int oldUnsortedTime = oldTimes[j];

                 if (oldUnsortedTime == newSortedTime){
                     oldIndex = j;
                     System.out.println(place + "  " +names[oldIndex] + "   "  + oldTimes[oldIndex] );
                     place++;//increment to next placement
                 }

             }  }

         }

    private void timeConvert(int[] times, String[] names){
        int[] oldTimes = new  int[times.length];
        int hours = times / 60; //since both are ints, you get an int
        int minutes = times % 60;
        System.out.printf("%d:%02d", hours, minutes);
             }



    }  

times不是int,而是int [],因此這是行不通的。 我認為您正在尋找類似的東西:

  private void timeConvert(int[] times, String[] names) {
    for (int time : times) {
      int hours = time / 60; //since both are ints, you get an int
      int minutes = time % 60;
      System.out.printf("%d:%02d\n", hours, minutes);
    }
  }

應該是這樣的:

私有void timeConvert(int []次,String []名稱){

int length = times.length ;
    for(int i = 0;i<length;i++){
         int hours = times[i] / 60;           
         int minutes = times[i] % 60;
         System.out.printf("%d:%02d", hours, minutes);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM