基本上,用户输入学生成绩,程序将显示并排列成部分(0-29分,30-39分,40-69分和70-100分)。如果用户输入高于100的任何值,则程序终止并输出结果。
我只是在垂直打印输出时遇到问题。
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Type in the students marks. Type above 100 to show output.");
int marks;
int starsfrom0to29 = 0;
int starsfrom30to39 = 0;
int starsfrom40to69 = 0;
int starsfrom70to100 = 0;
int counter = 0;
marks = input.nextInt();
while (marks < 100) {
//PUTTING MARKS INTO CATEGOERY
if ((marks >= 0) && (marks <= 29)) {
starsfrom0to29++;
}
if ((marks >= 30) && (marks <= 39)) {
starsfrom30to39++;
}
if ((marks >= 40) && (marks <= 69)) {
starsfrom40to69++;
}
if ((marks >= 70) && (marks <= 100)) {
starsfrom70to100++;
}
if (marks < 100) {
counter++;
}
marks = input.nextInt();
}
//PRINTING OUT NUMBER OF STARS
System.out.println();
System.out.print("0-29 ");
for (int x = 0; x < starsfrom0to29; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("30-39 ");
for (int x = 0; x < starsfrom30to39; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("40-69 ");
for (int x = 0; x < starsfrom40to69; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("70-100 ");
for (int x = 0; x < starsfrom70to100; x++) {
System.out.print("*");
}
System.out.println();
System.out.println();
}
将其水平打印出来。例如:如果输入数字:23,65,77,87,101,则显示:
0-29 *
30-39
40-69 *
70-100 **
但是,我需要找出如何垂直打印输出。基本上星星(星号)应该在垂直线上并且在标题之下。
答案 0 :(得分:2)
您需要的是一个循环,它会增加所有4个计数器,并在每次迭代中在同一行中打印1-4个星号。
int i = 0;
int j = 0;
int k = 0;
int l = 0;
System.out.println("0-29 30-39 40-69 70-100");
while (i<starsfrom0to29 || j < starsfrom30to39 || k < starsfrom40to69 || l < starsfrom70to100) {
if (i<starsfrom0to29) {
System.out.print(" * ");
i++;
} else {
System.out.print(" ");
}
if (j<starsfrom30to39) {
System.out.print(" * ");
j++;
} else {
System.out.print(" ");
}
if (k<starsfrom40to69) {
System.out.print(" * ");
k++;
} else {
System.out.print(" ");
}
if (l<starsfrom70to100) {
System.out.println(" * ");
l++;
} else {
System.out.println("");
}
}
答案 1 :(得分:1)
如果这是您要查找的输出类型:
0-29
*
*
30-39
*
*
...
使用以下内容交换您的PRINTING OUT NUMBERS OF STARS
代码:
System.out.println();
System.out.println("0-29 ");
for (int x = 0; x < starsfrom0to29; x++) {
System.out.println("*");
}
System.out.println("30-39 ");
for (int x = 0; x < starsfrom30to39; x++) {
System.out.println("*");
}
System.out.println("40-69 ");
for (int x = 0; x < starsfrom40to69; x++) {
System.out.println("*");
}
System.out.println("70-100 ");
for (int x = 0; x < starsfrom70to100; x++) {
System.out.println("*");
}
答案 2 :(得分:0)
如果这是您要查找的输出: -
Type in the students marks. Type above 100 to show output.
12
30
31
17
60
43
89
97
100
0-29 30-39 40-69 70-100
* * * *
* * * *
然后这是java代码: -
import java.util.*;
public class {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type in the students marks. Type above 100 to show output.");
int marks;
int starsfrom0to29 = 0;
int starsfrom30to39 = 0;
int starsfrom40to69 = 0;
int starsfrom70to100 = 0;
int counter = 0;
int array[]={0,0,0,0};
int i=0;
marks = input.nextInt();
while (marks < 100) {
//PUTTING MARKS INTO CATEGOERY
if ((marks >= 0) && (marks <= 29)) {
starsfrom0to29++;
}
if ((marks >= 30) && (marks <= 39)) {
starsfrom30to39++;
}
if ((marks >= 40) && (marks <= 69)) {
starsfrom40to69++;
}
if ((marks >= 70) && (marks <= 100)) {
starsfrom70to100++;
}
if (marks < 100) {
counter++;
}
marks = input.nextInt();
}
//PRINTING OUT NUMBER OF STARS
System.out.println();
System.out.print("0-29\t30-39\t40-69\t70-100");
for (int x = 0; x < starsfrom0to29; x++) {
array[0]=array[0]+1;
}
System.out.println();
for (int x = 0; x < starsfrom30to39; x++) {
array[1]=array[1]+1;
}
System.out.println();
for (int x = 0; x < starsfrom40to69; x++) {
array[2]=array[2]+1;
}
System.out.println();
for (int x = 0; x < starsfrom70to100; x++) {
array[3]=array[3]+1;
}
for(i=0;i<4;i++)
{
if(array[0]>0)
{
System.out.print("*\t");
array[0]=array[0]-1;
}
else System.out.print("\t");
if(array[1]>0)
{
System.out.print("*\t");
array[1]=array[1]-1;
}
else System.out.print("\t");
if(array[2]>0)
{
System.out.print("*\t");
array[2]=array[2]-1;
}
else System.out.print("\t");
if(array[3]>0)
{
System.out.print("*\t");
array[3]=array[3]-1;
}
else System.out.print("\t");
System.out.print("\n");
}
}
}
希望这会有所帮助!!
答案 3 :(得分:0)
这是代码。请注意,有更好的方法来处理整个标记范围的事情,但我不知道你的任务允许或不允许使用什么,所以我坚持简单的事情:
首先,您会找到您拥有的最大星数。这是为了知道要打印多少行星。如果你有至多3颗星,那么你需要打印三行。如果你有5,那么5,依此类推:
// Find max
int max = 0;
if ( starsfrom0to29 > max ) {
max = starsfrom0to29;
}
if ( starsfrom30to39 > max ) {
max = starsfrom30to39;
}
if ( starsfrom40to69 > max ) {
max = starsfrom40to69;
}
if ( starsfrom70to100 > max ) {
max = starsfrom70to100;
}
现在打印标题。这里的诀窍是每个标题必须是相同的宽度。由于最宽的是70-100
,宽度为6个字符,空格为7,因此我们必须将每个标题在7个字符的字段中右对齐。
System.out.println( " 0-29 30-39 40-69 70-100");
现在我们打印列。对于每个范围,如果在该行上仍然有要打印的星(即,如果星数仍然大于行数),我们打印星形,右对齐在7个字符的字段中。如果此列中没有更多星星,我们只打印空格。
for ( int i = 1; i <= max; i++ ) {
if ( starsfrom0to29 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom30to39 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom40to69 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom70to100 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
System.out.println();
}
请注意,所有命令都是System.out.print()
,因此它们不会打印行尾并只添加到同一行。出于这个原因,当我们完成一行时,我们需要使用一个System.out.println()
来给出整行的行尾。