java如何修改,从而不需要补全

问题遇到的现象和发生背景
import java.util.Scanner;

public class Histogram{ 
    
    private static final int SENTINAL = -999;          // sentinal value to signal endo of input
    private static final int MAX_NUMBERS = 20;         // maximum number of numbers to input
    private static final double UPPER_BOUND = 100.0;   // largest numbers accepted as data
    private static final double LOWER_BOUND = 0.0;     // smallest numbers accepted as adata
    private static final int NUM_BINS = 10;            // number of bins in range [0..100]

    private static final int BIN_SIZE = 100/NUM_BINS;           // size of each bin
   
    /*
     * Method to show an example of using StringBuilder.
     *
     * You will also notice that this method is called from the 
     * main function. 
     *
     */
    public static String getHeaderAsString( String me ) {

    // Create an instance of the StringBuilder class
    // which allows us to create an object of 
    // a series of strings that can then be converted 
    // into one large string via the toString method.
    //
        StringBuilder sb=new StringBuilder();

        sb.append( System.getProperty("line.separator") );
        sb.append( "Welcome to the Histogram Program " + me + "!" );
        me = getFirstName(me);
        sb.append( System.getProperty("line.separator") );
        sb.append( System.getProperty("line.separator") );
        sb.append( "This program will print out a histogram of the numbers" );
        sb.append( System.getProperty("line.separator") );
        sb.append( "input by you " + getFirstName(me) + "." );
        sb.append( System.getProperty("line.separator") );
        sb.append( System.getProperty("line.separator") );
        sb.append( "Please enter up to " + MAX_NUMBERS + " doubles or " + SENTINAL + " to stop input!" );
        sb.append( System.getProperty("line.separator") );

        return sb.toString();
    }

    /* 
     * Method to return the first name of the user in case
     * the full name was entered. 
     */
    public static String getFirstName(String name ) {
        // Note that add the " " to string to be sure
        // there is something to split
    return (name+" ").split(" ")[0]; 
    }

    /* 
     * local main test driver
     *
     */
    public static void main(String[] args) {  

    // Connect to the keyboard as the input stream
        Scanner userInput = new Scanner( System.in );

        System.out.print( "And who am I working with today? " );
        String user = userInput.nextLine();

        String heading = getHeaderAsString( user );

        // Print out welcome message
        System.out.println( heading ); 
        
        // Call the method which prompts the user
        // to input the numbers that will be used
        // to build the histogram.
        double[] numbers = inputNumbers( userInput );

    // Call the method to reate the array histogram
        int[] histogram = calculateHistogram( numbers );

    // Print the historgram
        System.out.println( toString( histogram ) );
    }
    public static int[] calculateHistogram(double []numbers){
        int histogram[] = new int[NUM_BINS];

//looping through numbers array

        for (int i = 0; i < numbers.length; i++) {

//incrementing the appropriate bins counter based on value

        if (numbers[i]<=LOWER_BOUND+BIN_SIZE&& LOWER_BOUND+BIN_SIZE1;

        }else if (numbers[i]<=LOWER_BOUND+(2*BIN_SIZE)&& LOWER_BOUND+(2*BIN_SIZE)<=UPPER_BOUND){
            histogram[NUM_BINS-(NUM_BINS-1)]+=1;

        }else if (numbers[i]<=LOWER_BOUND+(3*BIN_SIZE)&& LOWER_BOUND+(3*BIN_SIZE)<=UPPER_BOUND) {
            histogram[NUM_BINS-(NUM_BINS-2)]+=1;
    
        }else if (numbers[i]<=LOWER_BOUND+(4*BIN_SIZE)&& LOWER_BOUND+(4*BIN_SIZE)<=UPPER_BOUND){
            histogram[NUM_BINS-(NUM_BINS-3)]+=1;

        }else if (numbers[i]<=LOWER_BOUND+(5*BIN_SIZE)&& LOWER_BOUND+(5*BIN_SIZE)<=UPPER_BOUND) {
            histogram[NUM_BINS-(NUM_BINS-4)]+=1;

        } else if (numbers[i]<=LOWER_BOUND+(6*BIN_SIZE)&& LOWER_BOUND+(6*BIN_SIZE)<=UPPER_BOUND) {

            histogram[NUM_BINS-(NUM_BINS-5)]+=1;

        } else if (numbers[i]<=LOWER_BOUND+(7*BIN_SIZE)&& LOWER_BOUND+(7*BIN_SIZE)<=UPPER_BOUND) {

            histogram[NUM_BINS-(NUM_BINS-6)]+=1;

        } else if (numbers[i]<=LOWER_BOUND+(8*BIN_SIZE)&& LOWER_BOUND+(8*BIN_SIZE)<=UPPER_BOUND) {

            histogram[NUM_BINS-(NUM_BINS-7)]+=1;

        } else if (numbers[i]<=LOWER_BOUND+(9*BIN_SIZE)&& LOWER_BOUND+(9*BIN_SIZE)<=UPPER_BOUND) {

            histogram[NUM_BINS-(NUM_BINS-8)]+=1;

        } else if (numbers[i]<=LOWER_BOUND+(10*BIN_SIZE)&& LOWER_BOUND+(10*BIN_SIZE)<=UPPER_BOUND) {

            histogram[NUM_BINS-(NUM_BINS-9)]+=1;

        }

    }

    return histogram;

}

    public static int findBin( double num ) {
        int result=(int) (num/10);
        return result;
    }

    public static String toString( int [] histogram ) {
        StringBuilder sb=new StringBuilder();

        int BIN_SIZE=(int) ((UPPER_BOUND-LOWER_BOUND)/NUM_BINS);
        
        int temp=(int) ((UPPER_BOUND-LOWER_BOUND)/NUM_BINS);
        
        int tem=(int) LOWER_BOUND;

        for(int i=0;i"["+tem+".."+BIN_SIZE+"]: "+"       ");
            
            for(int j=0;j'*');
            
        }
            
        sb.append( System.getProperty("line.separator") );
            
        tem=BIN_SIZE;
            
        BIN_SIZE+=temp;
    }
        return sb.toString();
    }




    public static boolean validInput( double num ) {

        if(numUPPER_BOUND) {
        return false;
    }
        
        return true;
    }

    public static double[] inputNumbers( Scanner scan ) {
        double[] input=new double[MAX_NUMBERS]; 
        int i=0;
        while(idouble num=scan.nextDouble();
        if(num!=SENTINAL) {
            if(validInput(num)) {
            input[i]=num;
            i++;
                }
        }
        else { 
        break;
            }
        }
        return input;
    }
}

这一串代码在输出的数组不足20个的时候也会自动补全,如何修改从而只显示输入的数组个数的星号.

这个是我的代码运行的

img

这个是要实现的效果,不需要补全没有到达20的数组.

img

用代码块功能插入代码,请勿粘贴截图

那你需要有个变量记录数组中实际有效的元素数量才行。不能按数组的大小来计算啊
如下,增加一个有效数据数量的变量NUMS
然后在图表统计函数中,将for循环修改如下:
for (int i = 0; i < numbers.length; i++) {
改为
for (int i = 0; i < NUMS; i++) {

import java.util.Scanner;
 
public class Histogram{ 
    
    private static final int SENTINAL = -999;          // sentinal value to signal endo of input
    private static final int MAX_NUMBERS = 20;         // maximum number of numbers to input
    private static final double UPPER_BOUND = 100.0;   // largest numbers accepted as data
    private static final double LOWER_BOUND = 0.0;     // smallest numbers accepted as adata
    private static final int NUM_BINS = 10;            // number of bins in range [0..100]
    private static final int NUMS = 0;
    private static final int BIN_SIZE = 100/NUM_BINS;           // size of each bin
   
    /*
     * Method to show an example of using StringBuilder.
     *
     * You will also notice that this method is called from the 
     * main function. 
     *
     */
    public static String getHeaderAsString( String me ) {
 
    // Create an instance of the StringBuilder class
    // which allows us to create an object of 
    // a series of strings that can then be converted 
    // into one large string via the toString method.
    //
        StringBuilder sb=new StringBuilder();
 
        sb.append( System.getProperty("line.separator") );
        sb.append( "Welcome to the Histogram Program " + me + "!" );
        me = getFirstName(me);
        sb.append( System.getProperty("line.separator") );
        sb.append( System.getProperty("line.separator") );
        sb.append( "This program will print out a histogram of the numbers" );
        sb.append( System.getProperty("line.separator") );
        sb.append( "input by you " + getFirstName(me) + "." );
        sb.append( System.getProperty("line.separator") );
        sb.append( System.getProperty("line.separator") );
        sb.append( "Please enter up to " + MAX_NUMBERS + " doubles or " + SENTINAL + " to stop input!" );
        sb.append( System.getProperty("line.separator") );
 
        return sb.toString();
    }
 
    /* 
     * Method to return the first name of the user in case
     * the full name was entered. 
     */
    public static String getFirstName(String name ) {
        // Note that add the " " to string to be sure
        // there is something to split
    return (name+" ").split(" ")[0]; 
    }
 
    /* 
     * local main test driver
     *
     */
    public static void main(String[] args) {  
 
    // Connect to the keyboard as the input stream
        Scanner userInput = new Scanner( System.in );
 
        System.out.print( "And who am I working with today? " );
        String user = userInput.nextLine();
 
        String heading = getHeaderAsString( user );
 
        // Print out welcome message
        System.out.println( heading ); 
        
        // Call the method which prompts the user
        // to input the numbers that will be used
        // to build the histogram.
        double[] numbers = inputNumbers( userInput );
 
    // Call the method to reate the array histogram
        int[] histogram = calculateHistogram( numbers );
 
    // Print the historgram
        System.out.println( toString( histogram ) );
    }
    public static int[] calculateHistogram(double []numbers){
        int histogram[] = new int[NUM_BINS];
 
//looping through numbers array
 
        for (int i = 0; i < NUMS; i++) {
 
//incrementing the appropriate bins counter based on value
 
        if (numbers[i]<=LOWER_BOUND+BIN_SIZE&& LOWER_BOUND+BIN_SIZE<UPPER_BOUND) {
            histogram[NUM_BINS-NUM_BINS]+=1;
 
        }else if (numbers[i]<=LOWER_BOUND+(2*BIN_SIZE)&& LOWER_BOUND+(2*BIN_SIZE)<=UPPER_BOUND){
            histogram[NUM_BINS-(NUM_BINS-1)]+=1;
 
        }else if (numbers[i]<=LOWER_BOUND+(3*BIN_SIZE)&& LOWER_BOUND+(3*BIN_SIZE)<=UPPER_BOUND) {
            histogram[NUM_BINS-(NUM_BINS-2)]+=1;
    
        }else if (numbers[i]<=LOWER_BOUND+(4*BIN_SIZE)&& LOWER_BOUND+(4*BIN_SIZE)<=UPPER_BOUND){
            histogram[NUM_BINS-(NUM_BINS-3)]+=1;
 
        }else if (numbers[i]<=LOWER_BOUND+(5*BIN_SIZE)&& LOWER_BOUND+(5*BIN_SIZE)<=UPPER_BOUND) {
            histogram[NUM_BINS-(NUM_BINS-4)]+=1;
 
        } else if (numbers[i]<=LOWER_BOUND+(6*BIN_SIZE)&& LOWER_BOUND+(6*BIN_SIZE)<=UPPER_BOUND) {
 
            histogram[NUM_BINS-(NUM_BINS-5)]+=1;
 
        } else if (numbers[i]<=LOWER_BOUND+(7*BIN_SIZE)&& LOWER_BOUND+(7*BIN_SIZE)<=UPPER_BOUND) {
 
            histogram[NUM_BINS-(NUM_BINS-6)]+=1;
 
        } else if (numbers[i]<=LOWER_BOUND+(8*BIN_SIZE)&& LOWER_BOUND+(8*BIN_SIZE)<=UPPER_BOUND) {
 
            histogram[NUM_BINS-(NUM_BINS-7)]+=1;
 
        } else if (numbers[i]<=LOWER_BOUND+(9*BIN_SIZE)&& LOWER_BOUND+(9*BIN_SIZE)<=UPPER_BOUND) {
 
            histogram[NUM_BINS-(NUM_BINS-8)]+=1;
 
        } else if (numbers[i]<=LOWER_BOUND+(10*BIN_SIZE)&& LOWER_BOUND+(10*BIN_SIZE)<=UPPER_BOUND) {
 
            histogram[NUM_BINS-(NUM_BINS-9)]+=1;
 
        }
 
    }
 
    return histogram;
 
}
 
    public static int findBin( double num ) {
        int result=(int) (num/10);
        return result;
    }
 
    public static String toString( int [] histogram ) {
        StringBuilder sb=new StringBuilder();
 
        int BIN_SIZE=(int) ((UPPER_BOUND-LOWER_BOUND)/NUM_BINS);
        
        int temp=(int) ((UPPER_BOUND-LOWER_BOUND)/NUM_BINS);
        
        int tem=(int) LOWER_BOUND;
 
        for(int i=0;i<histogram.length;i++) {
 
            sb.append("["+tem+".."+BIN_SIZE+"]: "+"       ");
            
            for(int j=0;j<histogram[i];j++) {
            
            sb.append('*');
            
        }
            
        sb.append( System.getProperty("line.separator") );
            
        tem=BIN_SIZE;
            
        BIN_SIZE+=temp;
    }
        return sb.toString();
    }
 
 
 
 
    public static boolean validInput( double num ) {
 
        if(num<LOWER_BOUND|| num>UPPER_BOUND) {
        return false;
    }
        
        return true;
    }
 
    public static double[] inputNumbers( Scanner scan ) {
        double[] input=new double[MAX_NUMBERS]; 
        while(NUMS<MAX_NUMBERS) {
        double num=scan.nextDouble();
        if(num!=SENTINAL) {
            if(validInput(num)) {
            input[i]=num;
            NUMS++;
                }
        }
        else { 
        break;
            }
        }
        return input;
    }
}
 


package com.example.demo.csdn;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Histogram {

    private static final int SENTINAL = -999;          // sentinal value to signal endo of input
    private static final int MAX_NUMBERS = 20;         // maximum number of numbers to input
    private static final double UPPER_BOUND = 100.0;   // largest numbers accepted as data
    private static final double LOWER_BOUND = 0.0;     // smallest numbers accepted as adata
    private static final int NUM_BINS = 10;            // number of bins in range [0..100]

    private static final int BIN_SIZE = 100 / NUM_BINS;           // size of each bin

    /*
     * Method to show an example of using StringBuilder.
     *
     * You will also notice that this method is called from the
     * main function.
     *
     */
    public static String getHeaderAsString(String me) {

        // Create an instance of the StringBuilder class
        // which allows us to create an object of
        // a series of strings that can then be converted
        // into one large string via the toString method.
        //
        StringBuilder sb = new StringBuilder();

        sb.append(System.getProperty("line.separator"));
        sb.append("Welcome to the Histogram Program " + me + "!");
        me = getFirstName(me);
        sb.append(System.getProperty("line.separator"));
        sb.append(System.getProperty("line.separator"));
        sb.append("This program will print out a histogram of the numbers");
        sb.append(System.getProperty("line.separator"));
        sb.append("input by you " + getFirstName(me) + ".");
        sb.append(System.getProperty("line.separator"));
        sb.append(System.getProperty("line.separator"));
        sb.append("Please enter up to " + MAX_NUMBERS + " doubles or " + SENTINAL + " to stop input!");
        sb.append(System.getProperty("line.separator"));

        return sb.toString();
    }

    /*
     * Method to return the first name of the user in case
     * the full name was entered.
     */
    public static String getFirstName(String name) {
        // Note that add the " " to string to be sure
        // there is something to split
        return (name + " ").split(" ")[0];
    }

    /*
     * local main test driver
     *
     */
    public static void main(String[] args) {

        // Connect to the keyboard as the input stream
        Scanner userInput = new Scanner(System.in);

        System.out.print("And who am I working with today? ");
        String user = userInput.nextLine();

        String heading = getHeaderAsString(user);

        // Print out welcome message
        System.out.println(heading);

        // Call the method which prompts the user
        // to input the numbers that will be used
        // to build the histogram.
        double[] numbers = inputNumbers(userInput);

        // Call the method to reate the array histogram
        List<Category> categories = Category.build();
        for (double item : numbers) {
            categories.stream().filter(category -> category.add(item)).findAny().orElse(null);
        }

        // Print the historgram
        categories.stream().forEach(System.out::println);
    }

    public static int findBin(double num) {
        int result = (int) (num / 10);
        return result;
    }

    public static String toString(int[] histogram) {
        StringBuilder sb = new StringBuilder();

        int BIN_SIZE = (int) ((UPPER_BOUND - LOWER_BOUND) / NUM_BINS);

        int temp = (int) ((UPPER_BOUND - LOWER_BOUND) / NUM_BINS);

        int tem = (int) LOWER_BOUND;

        for (int i = 0; i < histogram.length; i++) {

            sb.append("[" + tem + ".." + BIN_SIZE + "]: " + "       ");

            for (int j = 0; j < histogram[i]; j++) {

                sb.append('*');

            }

            sb.append(System.getProperty("line.separator"));

            tem = BIN_SIZE;

            BIN_SIZE += temp;
        }
        return sb.toString();
    }


    public static boolean validInput(double num) {

        if (num < LOWER_BOUND || num > UPPER_BOUND) {
            return false;
        }

        return true;
    }

    public static double[] inputNumbers(Scanner scan) {
        double[] input = new double[MAX_NUMBERS];
        int i = 0;
        while (i < MAX_NUMBERS) {
            double num = scan.nextDouble();
            if (num != SENTINAL) {
                if (validInput(num)) {
                    input[i] = num;
                    i++;
                }
            } else {
                break;
            }
        }
        return input;
    }

    static class Category {
        private Double begin;
        private boolean beginInclude;
        private Double end;
        private List<Double> data;

        public static List<Category> build() {
            return IntStream.rangeClosed(0, 100 / 10 - 1).mapToObj(item -> new Category(item * 10d, item == 0 ? true : false, (item + 1) * 10d)).collect(Collectors.toList());
        }

        public boolean add(Double param) {
            boolean result = false;
            if (begin == param && beginInclude) {
                result = true;
                data.add(param);
            } else if (begin < param && param <= end) {
                result = true;
                data.add(param);
            }
            return result;
        }

        @Override
        public String toString() {
            return new StringBuilder().append(this.beginInclude ? "[" : "(").append(begin).append("...").append(end).append("]:").append(data.isEmpty() ? "" : IntStream.rangeClosed(0, data.size() - 1).mapToObj(item -> "*").collect(Collectors.joining())).toString();
        }

        public Category(Double begin, boolean beginInclude, Double end) {
            this.begin = begin;
            this.beginInclude = beginInclude;
            this.end = end;
            this.data = new ArrayList<>();
        }

        public double getBegin() {
            return begin;
        }

        public void setBegin(Double begin) {
            this.begin = begin;
        }

        public boolean isBeginInclude() {
            return beginInclude;
        }

        public void setBeginInclude(boolean beginInclude) {
            this.beginInclude = beginInclude;
        }

        public double getEnd() {
            return end;
        }

        public void setEnd(Double end) {
            this.end = end;
        }
    }
}



不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^