簡體   English   中英

基於Int數組打印星號

[英]Printing asterisk based on Int array

我正在嘗試創建一個循環,該循環將創建星號行,並且單行上的星號數量等於INT數組。 該循環將從索引0開始,直到數組末尾。 無法弄清楚如何編寫此代碼。 任何幫助表示贊賞。 到目前為止,這是我要提出的,而我仍然堅持下一步要做的事情。

int e = 0;
int[] yValuesInt = new int[yValues.length];
    for (z=0; z<yValues.length; z++)
    {
        yValuesInt[z] = (int) yValues[z]; // changing from double array to int
    }

    for (int i = 0; i<=arrayAmount-1; i++) // loop to continue until array is over
    {

    while (e<yValuesInt[z]) // loop that is creating asterisks based on int array
        {
            System.out.print("" + asterisk);
            e++;
        }
        System.out.println(": "  );
    }

我使用了自己的代碼,因為我不知道您的z變量是什么或arrayAmount變量等。

但是,這個概念確實很簡單,下面有一個小例子:

static int[] intArray = {5, 6, 10, 4};

public static void main(String[] args){
        for (int i = 0; i < intArray.length; i++){ // Iterate through the int array
            for(int j = 0; j < intArray[i]; j++){ // loop intArray[i] times
                System.out.print("" + "*");
            }
            System.out.println(": "  );
        }
    }

提供以下輸出:

*****: 
******: 
**********: 
****: 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM