简体   繁体   中英

How can I print a result from a nested for loop only once

I am trying to create a custom bar chart, where the user can enter the number of bars, size of bars and symbol that they would like to use to create each bar.

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("How many bars would you like to display?");
        int num_bars = scan.nextInt();

        int [] bars = new int[num_bars];
        String [] symbol = new String[num_bars];

        System.out.println("Specify the sizes of the bars: ");
        for(int i = 0; i < bars.length; i++) {
            bars[i] = scan.nextInt();
        }

        System.out.println("Specify the symbols to be used for the bars:");
        for(int i = 0; i < symbol.length; i++) {
            symbol[i] = scan.next();
        }

        int number = 1;
        for(int bar : bars) {
            System.out.print("\n" + number);
            for (String sym : symbol) {
                for (int size = 0; size < bar; size ++ ) {
                     System.out.print(sym +" "); 
            }
        System.out.println(" ");
        number++;
        }
        }   

    }
}

The result I am getting is displaying like this:

How many bars would you like to display?
2
Specify the sizes of the bars: 
8
4
Specify the symbols to be used for the bars:
%
#

1% % % % % % % %  
# # # # # # # #  

3% % % %  
# # # #  

but I am aiming for:

1 % % % % % % % %  
2 # # # #  

Can someone please help me?

There is no need to loop over the symbol[] array. it is redundant as you can print the symbol of each bar using its index.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.println("How many bars would you like to display?");
    int num_bars = scan.nextInt();

    int[] bars = new int[num_bars];
    String[] symbol = new String[num_bars];

    System.out.println("Specify the sizes of the bars: ");
    for (int i = 0; i < bars.length; i++) {
        bars[i] = scan.nextInt();
    }

    System.out.println("Specify the symbols to be used for the bars:");
    for (int i = 0; i < symbol.length; i++) {
        symbol[i] = scan.next();
    }

    int number = 1;
    for (int bar : bars) {
        System.out.print("\n" + number + " ");
        for (int size = 0; size < bar; size++) {
            System.out.print(symbol[number - 1] + " ");
        }
        System.out.println(" ");
        number++;
    }
}

you need to change your code to the following, because you are doing number++ before next number and no need to iterate symbol array again, simple use number integer value. Please find below:

       for (int bar : bars) {
        System.out.print("\n" + number);
        //for (String sym : symbol) {
            for (int size = 0; size < bar; size++) {
                System.out.print(symbol[number - 1] + " ");
        //              }
        //             System.out.println(" ");
        }
        number++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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