我需要帮助显示每只cat1到cat4的星星。我遇到的问题是,当你运行代码时,星号会显示在cat1到cat4下面。我想让星星显示在每只猫的侧面。
我的编码输出是:
读入标记 21
输入下一个学生分数 2
输入下一个学生分数 30
输入下一个学生分数 31
输入下一个学生分数 40
输入下一个学生分数 41
输入下一个学生分数 70
输入下一个学生分数 71
输入下一个学生分数 109 =当人输入超过100的数字时,while循环结束。
输入的标记总数8
0-29:2
*
*
30-39:2
*
*
40-69:2
*
*
70-100:2
*
*
我期望编码看起来像: 0-29: * *
30-39: * *
40-69: * *
70-100: * *
int cat1 = 0;
int cat2 = 0;
int cat3 = 0;
int cat4 = 0;
int mark;
int counter = 0;
Scanner input = new Scanner(System.in);
System.out.println("Read in a mark");
mark = input.nextInt();
while (mark <= 100 && mark >= 0) {
counter++;
if (mark >= 0 && mark <= 29) {
cat1++;
}
if (mark >= 30 && mark <= 39) {
cat2++;
}
if (mark >= 40 && mark <= 69) {
cat3++;
}
if (mark >= 70 && mark <= 100) {
cat4++;
}
System.out.println("Enter the next student mark");
mark = input.nextInt();
}
System.out.println("Total number of marks entered " + counter);
System.out.print("");
System.out.println("0-29: " + cat1);
for (int i = 0; i < cat1; i++) {
System.out.println("*");
}
System.out.print("");
System.out.println("30-39: " + cat2);
for (int i = 0; i < cat2; i++) {
System.out.println("*");
}
System.out.print("");
System.out.println("40-69: " + cat3);
for (int i = 0; i < cat3; i++) {
System.out.println("*");
}
System.out.print("");
System.out.println("70-100: " + cat4);
for (int i = 0; i < cat4; i++) {
System.out.println("*");
}
答案 0 :(得分:0)
试试这样..
String star = "";
for (int i = 0; i < cat1; i++) {
star = star +"*";
}
System.out.println("0-29: " + star);
答案 1 :(得分:0)
您应该将System.out.println
更改为System.out.print
并在每次循环结束后添加System.out.println();
System.out.print("");
System.out.print("0-29: " + cat1);
for (int i = 0; i < cat1; i++) {
System.out.print("*");
}
System.out.println();
System.out.print("");
System.out.print("30-39: " + cat2);
for (int i = 0; i < cat2; i++) {
System.out.print("*");
}
System.out.println();
System.out.print("");
System.out.print("40-69: " + cat3);
for (int i = 0; i < cat3; i++) {
System.out.print("*");
}
System.out.println();
System.out.print("");
System.out.print("70-100: " + cat4);
for (int i = 0; i < cat4; i++) {
System.out.print("*");
}
System.out.println();
可能你应该在需要的地方添加一些空格