繁体   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