简体   繁体   English

循环后需要Java print语句

[英]Need Java print statement after loop

I am working on the current program and I need the * print statement to print all at once at the end. 我正在处理当前程序,我需要* print语句在最后一次打印所有内容。 However, it keeps printing after each number is entered. 但是,它会在输入每个数字后继续打印。 I am new to java and any assistance would be appreciated. 我是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);
    }
}

Current Output: 电流输出:

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.

Expected Output: 预期产出:

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.

在退出之前将* print打开到右侧

You need an array to store to your various counts, here is one such solution - 你需要一个数组存储到你的各种计数,这是一个这样的解决方案 -

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);
}

Another approach would be to store your output into a StringBuilder , like so - 另一种方法是将输出存储到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);
}

Firstly, like the above answer stated, place the following for loop outside of the other loop: 首先,与上面给出的答案一样,将以下for循环放在另一个循环之外:

for (int i = 0; i < value; i++){
     System.out.print("*");
}

rather than inside, as have you done. 而不是在里面,就像你做的那样。

Secondly, I do not see the point of having this: 其次,我没有看到这样的意义:

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();
}

Taking your input twice seems to be redundant. 两次输入似乎是多余的。 Delete the first two statements, and instead use do while : 删除前两个语句,而不是使用do while

do{
       System.out.print("Input an integer between 1 and 30: ");
       value = input.nextInt();
}while(value < 1 || value > 30);

I believe you'll still get a logical error, however (i'm not sure what your program intends to do). 我相信你仍会得到一个逻辑错误,但是(我不确定你的程序打算做什么)。 You seem to want to print the total number of asterixis, so you'd either have to add the value to itself each time you take input, or you'll have to use a stringbuilder to append it within the loop. 您似乎想要打印星号的总数,因此您每次进行输入时都必须将值添加到自身,或者您必须使用字符串构建器将其附加到循环中。

You can't print all the stars continuously when you have variable as integer. 当变量为整数时,不能连续打印所有星。
U need to have arrays .. Because if at all u need to print all the * at the end,you need to have track of the count of stars in each line... If u use integer, by the time you enter the next line count, you will lose the previous line count... 你需要有数组..因为如果你需要打印最后的所有*,你需要跟踪每一行中的星数......如果你使用整数,那么当你进入下一行时行数,你将失去以前的行数......

Algorithm for your ques is: 你的问题的算法是:

1.declare an array 1.declare一个数组

2.get the total no of lines to be displayed 2.设置要显示的总行数

3.get the count of total no of stars in each line ( and store this in array ) 3.记住每行中星号总数的计数(并将其存储在数组中)

4.display the stars based on the count in this array. 4.根据此数组中的计数显示星星。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM