【问题标题】:How can I print a result from a nested for loop only once如何仅从嵌套的 for 循环中打印一次结果
【发布时间】:2020-09-04 12:18:04
【问题描述】:

我正在尝试创建一个自定义条形图,用户可以在其中输入他们想用来创建每个条形的条形图数量、条形图大小和符号。

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

    }
}

我得到的结果是这样的:

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% % % %  
# # # #  

但我的目标是:

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

有人可以帮帮我吗?

【问题讨论】:

    标签: java arrays loops iteration


    【解决方案1】:

    无需遍历 symbol[] 数组。这是多余的,因为您可以使用其索引打印每个柱的符号。

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

    【讨论】:

    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请编辑您的答案以添加解释并说明适用的限制和假设。
    【解决方案2】:

    您需要将代码更改为以下内容,因为您在下一个数字之前执行 number++ 并且无需再次迭代符号数组,只需使用数字整数值。 请在下面找到:

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

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 2013-09-16
      • 2022-10-06
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      相关资源
      最近更新 更多