首页 / JAVA / java-如何垂直打印输出
java-如何垂直打印输出
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何垂直打印输出,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2149字,纯文字阅读大概需要4分钟。
内容图文
![java-如何垂直打印输出](/upload/InfoBanner/zyjiaocheng/661/fbf34170dc4d4504af840c41bc30e053.jpg)
基本上,用户输入学生成绩,程序将显示它们并将其分为几部分(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 **
但是,我需要找出如何垂直打印输出.基本上,星号(星号)应位于垂直线上且在标题下方.
解决方法:
您需要的是一个循环,该循环将增加所有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("");
}
}
内容总结
以上是互联网集市为您收集整理的java-如何垂直打印输出全部内容,希望文章能够帮你解决java-如何垂直打印输出所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。