gpt4 book ai didi

java - 基于Int数组打印星号

转载 作者:太空宇宙 更新时间:2023-11-04 15:01:45 26 4
gpt4 key购买 nike

我正在尝试创建一个循环来创建星号行,单行上的星号数量等于 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(": " );
}
}

给出以下输出:

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

关于java - 基于Int数组打印星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com