如何使该数字时钟代码在Java中正常工作?

时间:2019-12-08 17:02:04

标签: java

过去2个小时,我一直在尝试完成此代码,但我一直遇到障碍,无法继续前进。最终结果应如下所示:在一个列中,应显示总和,在另一列中,还应显示该总和发生的次数。我尝试了许多不同的方法来使它起作用,但最终还是给我一个错误或显示了错误的结果。 此代码的基础是制定一个程序来显示加到12的时钟的小时和分钟的总和,并列出加到12的数字的次数。例如10:29,1 + 2 + 9 = 12。从现在开始,我需要在时钟上显示每个总和为23的总和,并显示总和为1到23之间任意数字的次数。

public class AidanMClock4{
  public static void main(String[] args){
    int hour = 0;
    int minutes = 0;
    int totalcount = 0;
    System.out.println("Here is the list of times");
    for(int h=1; h<=2; h++){
      for(int i=1; i<=12; i++){
        int leftSide = (i/10)+(i%10);
        for(int j=1; j<=59; j++){
          int rightSide = (j/10)+(j%10);
          int sum = leftSide+rightSide;
          if(sum ==2){
            totalcount++;
            System.out.println("Sum: " + sum + "\t Number of Times: " + totalcount);
          }
        }
      }
    }
  }
}

输出看起来像这样:

Here is the list of times
Sum: 2   Number of Times: 1
Sum: 2   Number of Times: 2
Sum: 2   Number of Times: 3
Sum: 2   Number of Times: 4
Sum: 2   Number of Times: 5
Sum: 2   Number of Times: 6
Sum: 2   Number of Times: 7
Sum: 2   Number of Times: 8

它需要显示以下内容:

Sum:1    Number of Times: 0
Sum:2    Number of Times: 8
Sum:3    Number of Times: 20

,依此类推直至23。 请帮帮我!

3 个答案:

答案 0 :(得分:1)

import java.io.*;

class Main 
{
  public static void main(String[] args) throws IOException 
  {   
     int h_digit_1 = 0;
     int h_digit_0 = 0;
     int m_digit_1 = 0;
     int m_digit_0 = 0;

     int sums[] = new int[24];
     for( int sum: sums )
         sum=0;

     int currentSum = 0;

     for( int h = 0 ; h < 12 ; h++ )
     {
         if( h < 10 )
         {
            h_digit_1 = 0;
            h_digit_0 = h;
         }
         else
         {
            h_digit_1 = 1;
            h_digit_0 = h%10;
         }

         for( int m = 0 ; m < 60 ; m++ )
         {
             if( m < 10 )
             {
                 m_digit_1 = 0;
                 m_digit_0 = m;
             }
             else
             {
                 m_digit_1 = m/10;
                 m_digit_0 = m - m_digit_1*10;       
             }

             currentSum = h_digit_1+h_digit_0+m_digit_1+m_digit_0;
             sums[currentSum]++;//since currentSum is always less than 24, its safe to use it as index!! also, sums[currentSum] represents count of occurrence of currentSum!!
         }
     }

     for( int i = 0 ; i<sums.length ; i++ )
     {
         System.out.println(i+" is the sum of hour and minute digits "+ sums[i]+ " number of times");
     }
  }    
}

答案 1 :(得分:0)

您似乎想使用一天的12小时时钟循环显示一天中所有可能的时间,然后每次对各个数字求和,然后计算每次求和的次数。

实现此类目标的适当数据结构是哈希表。这应该可以实现您想要的(无需以一种很好的方式打印出来)

public static void main(String[] args){
    Map<Integer, Integer> sums = new HashMap<>();
    for(int h=1; h<=2; h++){
        for(int i=1; i<=12; i++){
            int leftSide = (i/10)+(i%10);
            for(int j=1; j<=59; j++){
                int rightSide = (j/10)+(j%10);
                int sum = leftSide+rightSide;
                sums.put(sum, sums.getOrDefault(sum, 0) + 1);
            }
        }
    }
    System.out.println(sums);
}

答案 2 :(得分:0)

目前尚不清楚您要用代码实现什么。我假设您正在尝试遍历一天中的所有可能的小时-分钟组合,并计算每种组合的数字总和等于1到23的次数。

for(int h=1; h<=2; h++) 您似乎没有在代码中使用h,这意味着这样做只是使您的代码运行两次。

for(int i=1; i<=12; i++){
        int leftSide = (i/10)+(i%10);
        for(int j=1; j<=59; j++){
          int rightSide = (j/10)+(j%10);
          int sum = leftSide+rightSide;

对于内部循环的每次迭代,sum将是ij的数字之和。我认为这就是您要实现的目标。

if(sum ==2){
            totalcount++;
            System.out.println("Sum: " + sum + "\t Number of Times: " + totalcount);

只要sum等于2,就增加totalcount并打印出来。

如果您打算将所有可能的总和从1迭代到23,并打印ij的数字总和等于该数字的总和,请考虑执行以下更改

1)更改外部循环(h)以遍历可能的总和值。这可以通过将循环更改为for(int current_sum =1;current_sum<=23;current_sum++)来完成。

2)为该循环的每次迭代重置您的计数器。这可以通过使循环中的第一行概述为int totalcount = 0;

来完成。

3)将条件更改为字母总数等于当前总数而不是2。

4)每次外循环迭代只打印一次总和。请特别注意方括号,并确保打印行是外循环的最后一行。

5)将结果加倍,而不是计数两次。如果您希望同时计算AM和PM,只需打印出totalcount*2而不是totalcount

编辑: 如果遵循#2,则计算总和等于current_sum的次数的变量为totalcount。它应该看起来像这样:

int sum = leftSide+rightSide;
if (sum == current_sum){
    totalcount++;
}

和最外面的括号:

 System.out.println("Sum: " + current_sum + "\t Number of Times: " + totalcount);

编辑:仔细计算一下括号,在第一个for循环范围的结尾之前,应该放置打印语句。

 for(int current_sum=1; current_sum<=23; h++){
      for(int i=1; i<=12; i++){
        int leftSide = (i/10)+(i%10);
        for(int j=1; j<=59; j++){
          //Here is the body that counts the sums. More code goes here.
          if(current_sum == sum){
          totalcount++;
          }//if bracked
        }//Ending of the innermost loop
      }//brackets that middle loop(i)
     System.out.println("Sum: " + current_sum + "\t Number of Times: " + 
     totalcount);
    }//brackets that close the outer loop (current_sum)