【问题标题】:Java - Vertical Histogram troubleshoot: broken max value?Java - 垂直直方图故障排除:最大值损坏?
【发布时间】:2020-12-30 12:35:01
【问题描述】:

因此,经过反复试验,我将水平直方图至少部分转换为垂直直方图。

似乎不是读取最高使用次数,而是简单读取最高使用次数的值:

How many input values [max:30]?
5
Enter 5 numbers.
2
1
2
0
2
Number Occurrence
0 1
1 1
2 3

========= Vertical Bar ========
2    |     * 
1    | * * * 
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================

省略最大高度 3 并删除星号

How many input values [max:30]?
1
Enter 1 numbers.
5
Number Occurrence
5 1

========= Vertical Bar ========
5    |   
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================

不打印

5
Enter 5 numbers.
3
3
3
3
3
Number Occurrence
3 5

========= Vertical Bar ========
3    | * 
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================

打印错误的最大值、错误的星号数和错误的位置

How many input values [max:30]?
10
Enter 10 numbers.
5
4
3
2
1
1
2
3
4
5
Number Occurrence
1 2
2 2
3 2
4 2
5 2

========= Vertical Bar ========
5    |           
4    |           
3    |           
2    | * * * * * 
1    | * * * * * 
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================

为 5-3 添加空格

10
Enter 10 numbers.
2
2
3
3
3
4
4
4
4
1
Number Occurrence
1 1
2 2
3 3
4 4

========= Vertical Bar ========
4    |       * 
3    |     * * 
2    |   * * * 
1    | * * * * 
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================

按预期工作,但不是按设计


public class Histogram
{
    public static void main(String[] args) {
        //variables
        Scanner keyboard = new Scanner(System.in);
        int numInputs = 1, temp, maximum = 0;
        int[] numbers = new int[31];
        int[] count = new int[31];
        boolean success = false;

        //start of program
        System.out.println("How many input values [max:30]?");

        //while no valid input
        while (!success) {
            try {
                numInputs = keyboard.nextInt(); //get a number
                numInputChecker(numInputs);     //is it valid?
                success = true;                 //ok

            } catch (Exception e)                 //else get a new number
            {
                keyboard.nextLine();
                System.out.println("Whole numbers 1 through 30 only, please.");

            }
        }
        //reset the loop checker
        success = false;

        //read numbers to fill that array
        System.out.println("Enter " + numInputs + " numbers.");

        for (int i = 0; i < numInputs; i++)     //from 0 to max number
        {
            while (!success)                   //while no valid number
            {
                try {
                    numbers[i] = keyboard.nextInt();    //fill the current cell with a number
                    numberChecker(numbers[i]);          //is it valid?
                    success = true;                     //ok
                } catch (Exception e)                     //else get a new number
                {
                    keyboard.nextLine();
                    System.out.println("Whole numbers 0 through 9 only, please.");
                }
            }
            success = false;
        }

        //for cells not used
        for (int i = numInputs; i < numbers.length; i++) {
            numbers[i] = 10;    //fill with garbage data (to prevent false positive 0s)
        }

        //take the input and count each use of element
        for (int i : numbers)  //for 0 to max number
        {
            temp = i;  //get the current value of the cell
            count[temp]++;      //add the use of that value to a new array's cell
        }

        System.out.println("Number Occurrence");

        for (int i = 0; i < count.length; i++)   //from 0 to 9 (expected)
        {
            if ((count[i] > 0) && (count[i] <= 9))  //if cell not empty and has valid data
            {
                System.out.println(i + " " + count[i]);  //print the current cell and how many times it was used
            }
        }
        System.out.println();   //spacer

        //histogram segment

        //find the highest-used number
        for (int i : count)             //for each number
        {
            if(i > maximum)             //if greater than the current max
            {
                maximum = i;            //set to max
            }
        }

        System.out.println("========= Vertical Bar ========");
        for (int i = maximum; i > 0; i--)        //max through 1
        {
            if ((count[i] > 0) && (count[i] <=9))   //if has valid data
            {
                System.out.print((i) + "\t | ");      // print the number and a nice line for readability

                for (int j = 0; j < count.length; j++)      //for the number of times that number was used
                {
                    if ((count[j] > 0) && (count[j] <=9))   //if has valid data
                    {
                        if (count[j] >= i)                  //if that number the max
                        {
                            System.out.print("* ");            //print an asterisk
                        }
                        else
                            {
                                System.out.print("  ");     //"skip" and keep alignment
                            }
                    }
                }
                System.out.println();                   //make a new line
            }
        }

            System.out.println("===============================");  //footer
            System.out.println("| No | 0 1 2 3 4 5 6 7 8 9");
            System.out.println("===============================");
        }

    static void numInputChecker(int integer) throws Exception
    {
        if ((integer < 1) || (integer > 30))    //if 0 or negative, or if 31+
        {
            throw new Exception();              //say no
        }
    }

    static void numberChecker(int integer) throws Exception
    {
        if ((integer < 0) || (integer > 9)) //if negative or 10+
        {
            throw new Exception();          //say no
        }
    }

}

【问题讨论】:

    标签: java histogram vertical-alignment


    【解决方案1】:

    您的问题是您的星号输出逻辑中有太多 if 块。您决定不在基于数据的某些水平位置打印任何内容,而您真正想做的是在每个位置水平打印两个字符(一个空格加一个星号或两个空格),无论您是否看到任何那个位置的数字。因此,如果您取出除一个 if 之外的所有块来达到此目的,您的打印代码就会变得更简单并且做正确的事情:

    System.out.println("========= Vertical Bar ========");
    // for each count, starting from the max...
    for (int i = maximum; i > 0; i--) 
    {
        System.out.print((i) + "\t | ");          
    
        // for each number from 0 to the largest number we saw
        for (int j = 0; j < count.length; j++) 
        {
            // If the count at this position horizontally is greater than or
            // equal to the count vertically (the line number we're on), then
            // print an asterisk, else print a blank space.
            if (count[j] >= i).   
            {                    
                System.out.print("* ");           
            }
            else
            {
                System.out.print("  ");           
            }
        }
        System.out.println();                  
    }
    
    System.out.println("===============================");
    System.out.println("| No | 0 1 2 3 4 5 6 7 8 9");
    System.out.println("===============================");
    

    这是我用于测试的一个相当复杂的结果:

    输入:

    int[] numbers = { 1, 2, 3, 4, 5, 1, 1, 3, 3, 5, 5, 3, 3, 5, 5, 7 };
    

    结果:

    Number Occurrence
    1 3
    2 1
    3 5
    4 1
    5 5
    7 1
    
    ========= Vertical Bar ========
    5    |       *   *                                                   
    4    |       *   *                                                   
    3    |   *   *   *                                                   
    2    |   *   *   *                                                   
    1    |   * * * * *   *                                               
    ===============================
    | No | 0 1 2 3 4 5 6 7 8 9
    ===============================
    

    【讨论】:

    • 这几乎是完美的,谢谢!按照我的设置方式,我需要额外的 if 语句来检查数组中的垃圾数据,否则它会给我假的 10 秒,但我可以解决这个问题。
    • 哦,是的。我使用的是较小的numbers[],其中包含正确数量的项目进行测试。只需使用numInputs 值,您只需通过numbers[] 数组就可以构建count[] 数组。
    【解决方案2】:
    
    public class Histogram
    {
        public static void main(String[] args)
        {
            //variables
            Scanner keyboard = new Scanner(System.in);
            int numInputs = 1, temp, maximum = 0;
            int[] numbers = new int[31];
            int[] count = new int[31];
            boolean success = false;
    
            //start of program
            System.out.println("How many input values [max:30]?");
    
            //while no valid input
            while (!success)
            {
                try
                {
                    numInputs = keyboard.nextInt(); //get a number
                    numInputChecker(numInputs);     //is it valid?
                    success = true;                 //ok
    
                } catch (Exception e)                 //else get a new number
                {
                    keyboard.nextLine();
                    System.out.println("Whole numbers 1 through 30 only, please.");
    
                }
            }
            //reset the loop checker
            success = false;
    
            //read numbers to fill that array
            System.out.println("Enter " + numInputs + " numbers.");
    
            for (int i = 0; i < numInputs; i++)     //from 0 to max number
            {
                while (!success)                   //while no valid number
                {
                    try
                    {
                        numbers[i] = keyboard.nextInt();    //fill the current cell with a number
                        numberChecker(numbers[i]);          //is it valid?
                        success = true;                     //ok
                    } catch (Exception e)                   //else get a new number
                    {
                        keyboard.nextLine();
                        System.out.println("Whole numbers 0 through 9 only, please.");
                    }
                }
                success = false;
            }
    
            //for cells not used
            for (int i = numInputs; i < numbers.length; i++)
            {
                numbers[i] = -1;    //fill with garbage data (to prevent false positive 0s)
            }
    
            //take the input and count each use of element
            for (int i : numbers)  //for 0 to max number
            {
                if (i != -1)       //if valid data
                {
                    temp = i;           //get the current value of the cell
                    count[temp]++;      //add the use of that value to a new array's cell
                }
    
            }
    
            System.out.println("Number Occurrence");
    
            for (int i = 0; i < count.length; i++)   //from 0 to 9 (expected)
            {
                if (count[i] > 0)  //if cell has valid data
                {
                    System.out.println(i + " " + count[i]);  //print the current cell and how many times it was used
                }
            }
            System.out.println();   //spacer
    
            //histogram segment
    
            //find the highest-used number
            for (int k : count)              //for 0 to 9
            {
                if (k > maximum)             //if greater than the current max
                {
                    maximum = k;             //set to max
                }
            }
    
            System.out.println("========= Vertical Bar ========");
    
            for (int i = maximum; i > 0; i--) // from max to 1
            {
                System.out.print((i) + "\t | ");    //print the number and a spacer for visibility
    
    
                for (int j = 0; j < count.length; j++)   // from 0 to max
                {
    
                        if (count[j] >= i)                    // If the count at this position horizontally is greater than or
                                                              // equal to the count vertically (the line number we're on)
                        {
                            System.out.print("* ");           //print an asterisk
                        }
                        else
                            {
                                System.out.print("  ");       //else print a blank
                            }
                }
                System.out.println();                         //spacer
            }
            //footer
            System.out.println("===============================");
            System.out.println("| No | 0 1 2 3 4 5 6 7 8 9");
            System.out.println("===============================");
            }
    
        static void numInputChecker(int integer) throws Exception
        {
            if ((integer < 1) || (integer > 30))    //if 0 or negative, or if 31+
            {
                throw new Exception();              //say no
            }
        }
    
        static void numberChecker(int integer) throws Exception
        {
            if ((integer < 0) || (integer > 9)) //if negative or 10+
            {
                throw new Exception();          //say no
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2015-07-10
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多