我正在尝试以小时和分钟(00:00)为单位输出代码中的时间,需要帮助.这就是我到目前为止所拥有的



我有我的代码,但在解决方案 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:%02dn", hours, minutes);
    }
  }

它应该是这样的:

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

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);
    }
}

最新更新