我正在处理当前程序,我需要* print语句在最后一次打印所有内容。但是,它会在输入每个数字后继续打印。我是java的新手,我们将不胜感激。
import java.util.Scanner;
public class barPrint
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int counter = 0;
int value = 0;
// counter loop for 5 inputs
for (counter = 0; counter < 5; counter++){
System.out.print("Input an integer between 1 and 30: ");
value = input.nextInt();
while(value < 1 || value > 30){
System.out.print("Input an integer between 1 and 30: ");
value = input.nextInt();
}
// print the asterisks based on value
for (int i = 0; i < value; i++){
System.out.print("*");
}
System.out.println();
}
System.exit (0);
}
}
当前输出:
Input an integer between 1 and 30: 25
*************************
Input an integer between 1 and 30: 22
**********************
Input an integer between 1 and 30: 1
*
Input an integer between 1 and 30: 12
************
Input an integer between 1 and 30: 10
**********
Process finished with exit code 0.
预期产出:
Input an integer between 1 and 30: 25
Input an integer between 1 and 30: 22
Input an integer between 1 and 30: 1
Input an integer between 1 and 30: 12
Input an integer between 1 and 30: 10
*************************
**********************
*
************
**********
Process finished with exit code 0.
答案 0 :(得分:1)
在退出
之前将* print打开到右侧答案 1 :(得分:1)
你需要一个数组存储到你的各种计数,这是一个这样的解决方案 -
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// values holds room for 5 inputs
int[] values = new int[5];
for (int counter = 0; counter < values.length; counter++) {
System.out.print("Input an integer between 1 and 30: ");
System.out.flush();
int value = input.nextInt(); // <-- declare value here.
while (value < 1 || value > 30) {
System.out.print("Input an integer between 1 and 30: ");
System.out.flush();
value = input.nextInt();
}
values[counter] = value; // <-- store it in the values array.
}
// Use the for each operator to iterate over the value(s)...
for (int value : values) {
// print the asterisks based on value
for (int i = 0; i < value; i++) {
System.out.print("*");
}
System.out.println();
}
System.out.flush();
System.exit(0);
}
另一种方法是将输出存储到StringBuilder,如此 -
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
for (int counter = 0; counter < 5; counter++) {
System.out.print("Input an integer between 1 and 30: ");
System.out.flush();
int value = input.nextInt();
while (value < 1 || value > 30) {
System.out.print("Input an integer between 1 and 30: ");
System.out.flush();
value = input.nextInt();
}
for (int i = 0; i < value; i++) {
sb.append("*");
}
sb.append("\n");
}
System.out.println(sb.toString());
System.out.flush();
System.exit(0);
}
答案 2 :(得分:1)
首先,与上述答案一样,将以下for
循环放在另一个循环之外:
for (int i = 0; i < value; i++){
System.out.print("*");
}
而不是内部,就像你做的那样。
其次,我没有注意到这一点:
System.out.print("Input an integer between 1 and 30: ");
value = input.nextInt();
while(value < 1 || value > 30){
System.out.print("Input an integer between 1 and 30: ");
value = input.nextInt();
}
两次输入似乎是多余的。删除前两个语句,改为使用do while
:
do{
System.out.print("Input an integer between 1 and 30: ");
value = input.nextInt();
}while(value < 1 || value > 30);
我相信你仍然会遇到逻辑错误,但是(我不确定你的程序打算做什么)。您似乎想要打印星号的总数,因此您每次进行输入时都必须将值添加到自身,或者您必须使用字符串构建器将其附加到循环中。
答案 3 :(得分:0)
当变量为整数时,无法连续打印所有星星 你需要有阵列.. 因为如果你需要在最后打印所有*,你需要跟踪每行中的星数... 如果您使用整数,那么当您输入下一行数时,您将丢失先前的行数......
问题的算法是:
1.declare a array
2.设定要显示的总行数
3.计算每行中星号的总数(并将其存储在数组中)
4.根据此阵列中的计数显示星星。