基本上希望用户设置多个条形,然后填写设定量的高度。希望没有太多的代码丢失。
System.out.println("Enter number of bars (min 1/max 6:");
int[] barHeights = new int[7];
for (int i =0;i<barHeights.length;i++){
System.out.println("Enter Height of bar(min 1/max 7:");
input.nextInt();
}
答案 0 :(得分:1)
//Get number of bars
int numberOfBars = input.nextInt();
if(numberOfBars <= 0 )
{
System.out.println("Invaild entry");//Thanks @Task for pointing it out
return; // Replace with suitable return
}
//Create an array
int[] bars = new int[numberOfBars];
//Get the values
for (int i =0;i<bars.length;i++)
{
bars[i] = input.nextInt();
}
答案 1 :(得分:0)
您应先获取数组大小,然后填写
System.out.println("Enter number of bars (min 1/max 6:");
int size = input.nextInt();
int[] barHeights = new int[size ];
for (int i =0;i<barHeights.length;i++){
System.out.println("Enter Height of bar(min 1/max 7:");
barHeights [i]=input.nextInt();
}