【问题标题】:How to use for loop to print star for the size of an element in an array如何使用for循环为数组中元素的大小打印星号
【发布时间】:2015-04-27 03:40:15
【问题描述】:

我正在从文本文件中读取整数值,并将它们存储在 10 个 ex、1-10,11-20 的组中,就像这样存储在一个数组中。我需要在一行中打印一个星号,表示每组中有多少个数字:

1-10 *******

11-20************

我已经编写了所有代码,但无法正确打印输出。我尝试使用嵌套的 for 循环,但运气不佳。

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        int fileInts;
        Scanner scan = new Scanner(new File("Histogram.txt"));
        int[] count = new int[10];

        // while loop
        while (scan.hasNext()) {
            // find next line
            String num = scan.next();
            fileInts = Integer.parseInt(num);

            if (fileInts > 0 && fileInts < 11) {
                count[0]++;
            } else if (fileInts > 10 && fileInts < 21) {
                count[1]++;
            } else if (fileInts > 20 && fileInts < 31) {
                count[2]++;
            } else if (fileInts > 30 && fileInts < 41) {
                count[3]++;
            } else if (fileInts > 40 && fileInts < 51) {
                count[4]++;
            } else if (fileInts > 50 && fileInts < 61) {
                count[5]++;
            } else if (fileInts > 60 && fileInts < 71) {
                count[6]++;
            } else if (fileInts > 70 && fileInts < 81) {
                count[7]++;
            } else if (fileInts > 80 && fileInts < 91) {
                count[8]++;
            } else if (fileInts > 90 && fileInts < 101) {
                count[9]++;
            }
        }

        for (int s : count) {
            {
                for (int c = 0; c < count[c]; c++) {
                    System.out.print("*");
                    System.out.println();
                    s++;
                }
            }
            //System.out.println(count[]);
        }
    }
}

【问题讨论】:

  • 既然你要实现直方图,为什么在内部循环中调用System.out.println()
  • 我补充说,下一组星星将位于不同的行

标签: java arrays loops


【解决方案1】:

首先,你应该只使用

count[fileInts / 10]++;

而不是这个长长的 if-else 块。整数除法将始终只给您整数值,而不是浮点数。如果您需要剩余值,请使用模 %。

回答您的问题:您的第二个循环只会遍历列表中的元素,而不是每个字符。你期望例如21 是 2 个字符,但情况并非如此,因为您只计算整数 - 不计算每个整数中的小数。

【讨论】:

  • count[10/10]++是2,没有按照用户的意愿放在数组的0索引中
  • 考虑到 OP 的要求,count[ (fileInts-1)/10 ] 会更准确。
  • 谢谢我真的更担心输出
【解决方案2】:

你的循环需要是这样的:

for (int s : count) {
    {
        for (int c = 0; c < s; c++) {
            System.out.print("*");
        }
        System.out.println();
    }
}

用简单的英语思考这个过程:

  1. 对于范围内的每个整数,
    1. 打印星号
    2. 重复直到星数 == 当前整数
  2. 重复循环

其他说明:

System.out.println(); 内部循环在每个星号之后打印一个新行。那不是你想要的。您确实希望在打印一个范围的星星后打印一个新行。

在循环中,您将计数器变量ccount[c] 进行比较,这没有意义。您需要将它与您正在使用的当前整数进行比较,您已将其定义为s


当您试图推理某个进程在程序中的工作方式时,写下一组输入数据并手动处理它会有所帮助。我知道我通过洗一副纸牌并通过它们学习了常见的排序算法。

【讨论】:

    【解决方案3】:

    要在计算范围内每个数字出现的次数后循环遍历数组,请执行以下操作:

    for (int i = 0; i < count.length; i++)
    {
        for (int j = 0; j < count[i]; j++)
            System.out.print("*");
        System.out.println("");
    }
    

    但这还有另一个问题,即您无法看到自己在数哪些数字。用数字范围制作一个字符串数组可能是一个想法:

    String[] ranges = {"1 - 10: ", "11 - 20: ", ... , "90 - 100: " };
    

    然后换你的for循环先打印出合适的范围:

    for (int i = 0; i < count.length; i++)
    {
        System.out.print(ranges[i]);
        for (int j = 0; j < count[i]; j++)
            System.out.print("*");
        System.out.println();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      相关资源
      最近更新 更多