HISTOGRAM(阵列=星输出) [英] HISTOGRAM (Array = Stars Output)

查看:95
本文介绍了HISTOGRAM(阵列=星输出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

历史记录:

我的代码有问题。.

预期的输出必须是这样的:

HISTOGRAM:
I have problem in my code..
The Intended Output must be something Like this:

输出:

0 8 ********  
1 6 ******   
2 3 ***  
3 7 *******

但是我的节目显示:

0 8 *******************************    
1 6 *******************************
2 3 *******************************  
3 7 *******************************

我已经搜索并将其与我的代码进行比较,但没有任何帮助。.

I've searched and compare it to my code but nothing really helps me though..

请看一下我的代码并提供一些建议和评论

Can you kindly take a look at my code and give some suggestions, and comments on

我如何正确编码预期的输出。.

how could I code the intended output properly..

任何帮助...

    public static void main(String args[]) {
        {

        StringBuilder stringBuilder = new StringBuilder();

             int n = 0;
            n  = Integer.parseInt(JOptionPane.showInputDialog("Enter value"));

            int[] arr = new int[n];
             String stars = "";
             int input = 0;



            for(int c = 0; c<n; c++ ){
            input  = Integer.parseInt(JOptionPane.showInputDialog("Enter number"));
            arr[c]=input;


             for(int i=0; i<input; i++){ 
                        stringBuilder.append("*"); 
                 }


                 }
            for(int i=0; i<input; i++){ 
                stringBuilder.append("*"); 
            }

                for (int o = 0; o<n ; o++){ 
                stars = stringBuilder.toString();
                System.out.println( o +" "+arr[o]+" "+stars);
                }



        }
        }
    }


推荐答案

每次在构建器对象中附加 * 时,请清除先前的内容。您可以使用 stringBuilder.setLength(0);

Every time you append the * in the builder object, clear the previous content. You can use stringBuilder.setLength(0);

import javax.swing.*;

public class Prop {
  public static void main(String args[]) {

    StringBuilder stringBuilder = new StringBuilder();

    int n = 0;
    n  = Integer.parseInt(JOptionPane.showInputDialog("Enter value"));

    int[] arr = new int[n];
    String stars = "";
    int input = 0;

    for(int c = 0; c<n; c++ ){
      input  = Integer.parseInt(JOptionPane.showInputDialog("Enter number"));
      arr[c]=input;

      for(int i=0; i<input; i++){
        stringBuilder.append("*");
      }
      stars = stringBuilder.toString();
      System.out.println( c +" "+arr[c]+" "+stars);
      stringBuilder.setLength(0);             // Reset the `stringBuilder` once pattern is written
    }
  }
}

输出:

0 8 ********  
1 6 ******  
2 3 ***  
3 7 *******  

这篇关于HISTOGRAM(阵列=星输出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆