我最终完成了我的直方图程序,并希望获得最后润色的帮助,我需要我的程序拒绝非数值添加,然后显示一个说重试或其他东西的println。到目前为止,我已经想出了这个,但我不确定我是否将循环放在正确的位置?
package hcw;
import java.util.Scanner;
public class HCW {
public static void main(String[] args) {
Scanner MK = new Scanner(System.in);
int marks = 0, count, count2, count3, count4 = 0;
int Range = 0, Range2 = 0, Range3 = 0, Range4 = 0;
int totalmarks = 0;
do {
System.out.println("Enter mark: ");
marks = MK.nextInt();
while (marks != marks);
int biggestmark = 0;
int smallestmark = 100;
while ((marks < 101) && (marks >= 0)) {
if (marks <= 29) {
Range++;
} else if ((marks <= 39) && (marks >= 30)) {
Range2++;
} else if ((marks <= 69) && (marks >= 40)) {
Range3++;
} else if ((marks <= 70) && (marks >= 101)) {
Range4++;
}
totalmarks = totalmarks + marks;
if (marks > biggestmark) {
biggestmark = marks;
}
if (marks < smallestmark) {
smallestmark = marks;
}
System.out.println("Enter mark: ");
marks = MK.nextInt();
}
System.out.println("0 - 29 ");
for (count = 0; count < Range; count++) {
System.out.print("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("30 - 39 ");
for (count2 = 0; count2 < Range2; count2++) {
System.out.print("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("40 - 69 ");
for (count3 = 0; count3 < Range3; count3++) {
System.out.print("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("70 - 100 ");
for (count4 = 0; count4 < Range3; count4++) {
System.out.print("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("");
int totalstudents = count + count2 + count3 + count4;
int averagemark = totalmarks / totalstudents;
int studentpass = count3 + count4;
System.out.println("Students in total are " + totalstudents);
System.out.println("Average Mark is " + averagemark);
System.out.println("Number of Students who passed (with a mark over 40) is " + studentpass);
System.out.println("Highest Mark Awarded is " + biggestmark);
System.out.println("Smallest Mark Awarded is " + smallestmark);
System.out.println(totalstudents + smallestmark);
{
System.out.println("0 - 29 ");
for (count = 0; count < Range; count++) {
System.out.println("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("30 - 39 ");
for (count2 = 0; count2 < Range2; count2++) {
System.out.println("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("40 - 69 ");
for (count3 = 0; count3 < Range3; count3++) {
System.out.println("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("70 - 100 ");
for (count4 = 0; count4 < Range3; count4++) {
System.out.println("*");// if you want * vertically add ln to print
}
System.out.println("");
System.out.println("");
}
System.out.println("");
System.out.println("");
}
}
非常感谢。
答案 0 :(得分:0)
您应首先修复终止String或int,因为在某些输入后您想要停止。 为此,您可以首先获取整数学生的整数值,然后循环这些值。 或者你可以获取字符串值,如果没有终止,可以从字符串解析到int 一个字符串。 例如。输入标记: -
1 2 3 做完了。完成是终止字符串。
对于非数字值,您可以添加try-catch块,在那里打印消息,然后再返回到同一点。