0

我有我的代码,但在解决方案 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);
             }



    }  
4

2 回答 2

0

它应该是这样的:

私人无效时间转换(int []次,字符串[]名称){

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);
    }
}
于 2013-10-30T03:00:14.980 回答
0

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);
    }
  }
于 2013-10-30T02:54:35.697 回答