我有一个任务,我必须在一个数组中插入随机数,如果我把0作为我的数字或者如果已经达到数组长度那么循环应该停止,但是我被卡住了。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] a1 = new int[100];
int i = 1;
boolean run = true;
while(run){
if(i < a1.length && i != 0){
System.out.println("Insert number: (0-end)");
a1[i] = scan.nextInt();
}else if(i == 0){
run = false;
}
}//while
答案 0 :(得分:1)
更改此行:
while(true){
要:
while(run){
答案 1 :(得分:0)
while循环应该像
while(true){
System.out.println("Ange tal: (0-avsluta)");
int temp = scan.nextInt();
if(i == a1.length || temp == 0)
{
break;
}
else
{
a1[i] = temp;
i++;
}
}
答案 2 :(得分:0)
如果分支则删除else并添加:
if(i==0){
run = false;
}
in if branch。
答案 3 :(得分:0)
试试这个(这里的循环比同时更易读):
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] a1 = new int[100];
for(int i=1; i< 100; i++) {
System.out.println("Insert number: (0-end)");
int nextInt = scan.nextInt();
if(nextInt == 0) break;
a1[i] = nextInt;
}
scan.close();
}
答案 4 :(得分:0)
这将按预期工作:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Insert size of array: ");
int n = scan.nextInt();
int index = 0;
int value = -1;
int[] a1 = new int[n]; //read the number of elements, because after you stop reading will be 0. If you leave it as int int[100] you might have all of them 0
while (true) {
System.out.println("Input number: ");
value = scan.nextInt();
if (index != n && value != 0) {
a1[index] = value;
index++;
} else
break;
}
System.out.println("Content of array a1 of size:"+n +" elements:");
for(int i=0;i<a1.length;i++){
System.out.println("a1["+i+"] = "+a1[i]);
}
}
答案 5 :(得分:0)
这个while循环不会因为你没有增加变量i的值而停止。所以它将是无限循环......我将i作为数组a1的索引变量...而用户输入的数字应该是根据你的问题进行检查......我已经编辑了代码,请立即查看...
Scanner scan = new Scanner(System.in);
int d=0;
int [] a1 = new int[100];
int i = 1;
boolean run = true;
while(run){
if(i < a1.length && i != 0){
System.out.println("Ange tal: (0-avsluta)");
d = scan.nextInt();
if(d==0)
run=false;
a1[i]=d;
i++
}//while
}
答案 6 :(得分:0)
使用: -
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] a1 = new int[100];
int i = 0;
while(true){
if(i < a1.length){
System.out.println("Insert number: (0-end)");
a1[i] = scan.nextInt();
if(a1[i]==0)
break;
else
i++;
}
else
break;
}//while ends here
}//End of main
如果您正在寻找最简单的方法,我建议您尝试这样做: -
for(int i=0;i<100;i++){
System.out.println("Enter the number,0 to exit");
a1[i]=scan.nextInt();
if(a[i]==0)
break;
}
答案 7 :(得分:0)
好了,我有
扫描仪扫描=新扫描仪(System.in);
int [] a1 = new int[100];
int i = 0;
int tal;
int mellan;
int antal;
boolean run = true;
while(run){
System.out.println("Ange tal: (0-avsluta)");
tal = scan.nextInt();
if( i == a1.length || tal == 0){
break;
}else{
a1[i] = tal;
i++;
}
}//while
如何显示放入数组的所有数字?
答案 8 :(得分:0)
尝试此代码
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] a1 = new int[10];
int i = 0;
boolean run = true;
int inputnum;
while(run){
System.out.println("Ange tal: (0-avsluta)");
inputnum =scan.nextInt();
if(i<a1.length)
{
if(inputnum==0)
{
run=false;
break;
}
else{
a1[i]=inputnum;
i++;
}
}
else
{
System.out.println("Array is full");
run=false;
break;
}
}
System.out.println("You are out of program");
}