增量输出显示少于输入

时间:2017-11-22 14:47:50

标签: java

package javahistogram;

import java.util.Scanner;

public class JavaHistogram {

    public static void main(String[] args) {
        int sum = 0;
        int count = 0;
        int mark = 0;
        int largest = 0, smallest = 0;
        int rangeOne = 0, rangeTwo = 0, rangeThree = 0, rangeFour = 0;

        System.out.println("Enter marks, -1 to finish: ");
        Scanner in = new Scanner(System.in);

// Process data until the sentinel is entered
        if (in.hasNextInt()) {
            {

                while (mark != -1) {
                    mark = in.nextInt();

                    if (mark < -1 || mark > 100) {
                        System.out.println("Error: Enter Valid Input Type: Numbers from 0 - 100 for Marks. Or -1 to finish.");
                    } else if (mark != -1) {
                        sum = sum + mark;
                        count++;

                        if (count == 1) {
                            largest = mark;
                            smallest = mark;
                        } else {
                            if (mark > largest) {
                                largest = mark;
                            }
                            if (mark < smallest) {
                                smallest = mark;
                            }

                            if (mark >= 0 && mark <= 29) {
                                rangeOne++;
                            } else if (mark >= 30 && mark <= 39) {
                                rangeTwo++;
                            } else if (mark >= 40 && mark <= 69) {
                                rangeThree++;
                            } else if (mark >= 70 && mark <= 100) {
                                rangeFour++;
                            }

                        }
                    }
                }
            }
        } else {
            System.out.println("Error: Enter Valid Input Type: Integer");
        }

        for (int i = 1; i <= rangeOne; i++) {
            System.out.print("*");
        }

        for (int i = 1; i <= rangeTwo; i++) {
            System.out.println("*");
        }

        for (int i = 1; i <= rangeThree; i++) {
            System.out.println("*");
        }

        for (int i = 1; i <= rangeFour; i++) {
            System.out.println("*");
        }

        System.out.println(rangeOne + rangeTwo + rangeThree + rangeFour + " students in total");
        System.out.println("Largest mark is: " + largest);
        System.out.println("Smallest mark is: " + smallest);
        if (count > 0) {
            double average = sum / count;
            System.out.println("Average marks: " + average);
        } else {
            System.out.println("No data");
        }

        for (int i = 1; i <= rangeThree; i++) {
            System.out.println("Number of students passed " + i);
        }

    }
}

在我的增量无法正常工作时出现错误。

例如,如果我输入;

0, 0, 39, 39, 100

应显示5颗星。

然而,目前只会显示4颗星。

我想知道我的增量代码在哪里出错?

有了这个,另一个问题是我的程序的格式。

例如,在0,0, 39, 39, 100的情况下 它会显示如下:

0 - 29: **  
30 - 39: **  
70 - 100: *  

目前,它显示为

0 - 29: *  
0 - 29: *  
30 - 39: *  
30 - 39: *  
70 - 100: *  

感谢。 :)

3 个答案:

答案 0 :(得分:0)

在你的第一个分支中没有添加任何内容。您应该将其更改为独立于范围计数器检查最大值和最小值,例如

if (count == 1) {
    largest = mark;
    smallest = mark;
} else {
    if (mark > largest) {
        largest = mark;
    }
    if (mark < smallest) {
        smallest = mark;
    }
} //Ending bracket is moved here
if (mark >= 0 && mark <= 29) {
    rangeOne++;
} else if (mark >= 30 && mark <= 39) {
    rangeTwo++;
} else if (mark >= 40 && mark <= 69) {
    rangeThree++;
} else if (mark >= 70 && mark <= 100) {
    rangeFour++;
}

至于问题的第二部分,格式化输出可以使用System.out.print()继续在同一行打印,\n手动打破行。这意味着要在同一条线上打印名称和星星,就像这样。

System.out.print("0 - 29:");
for (int i = 1; i <= rangeOne; i++) {
    System.out.print("*");
}
System.out.print("\n30 - 39:");
for (int i = 1; i <= rangeTwo; i++) {
    System.out.print("*");
}
...

答案 1 :(得分:0)

我认为您的问题在于尝试在循环中打印println时使用*

我建议使用do...while并略微修改代码:

do {
     mark = in.nextInt();
     if (mark >= 0 && mark <= 29) {
        rangeOne++;
     } else if (mark >= 30 && mark <= 39) {
        rangeTwo++;
     } else if (mark >= 40 && mark <= 69) {
        rangeThree++;
     } else if (mark >= 70 && mark <= 100) {
        rangeFour++;
     }
 } while (mark != -1);

打印循环使用print,而不是println

System.out.print("0-29: ");
        for (int i = 1; i <= rangeOne; i++) {
            System.out.print("*");
        }

        System.out.println();
        System.out.print("30-39: ");
        for (int i = 1; i <= rangeTwo; i++) {
            System.out.print("*");
        }

        System.out.println();
        System.out.print("40-69: ");
        for (int i = 1; i <= rangeThree; i++) {
            System.out.print("*");
        }

        System.out.println();
        System.out.print("70-100: ");
        for (int i = 1; i <= rangeFour; i++) {
            System.out.print("*");
        }
        System.out.println();

注意:您必须再次使用此检查以包含任何无效输入。

Try it online!

答案 2 :(得分:0)

问题出在这里

  if (count == 1) {
                        largest = mark;
                        smallest = mark;
                    } else {
                        if (mark > largest) {
                            largest = mark;
                        }
                        if (mark < smallest) {
                            smallest = mark;
                        }

                        if (mark >= 0 && mark <= 29) {
                            rangeOne++;
                        }

它的作用是检查第一个数字,如果count为1,它永远不会转到rangeone ++的else,尝试将最小和最大的逻辑用于计数。