2016-11-24 52 views
0

我想创建一个循环,要求扫描器在数组中以一定数量输入每个数字(我在考虑10)。有什么建议么?为阵列扫描器创建一个循环

import java.util.Scanner; 

public class AssignSeven 
{ 
    public static void main(String[] args) 
    { 
     int [] array1 = new int[10]; 
     System.out.println("Enter 10 numbers"); 
     Scanner sc = new Scanner(System.in); 
     int a = sc.nextInt(); 

     for (int i = 0; i < 9; i++) 
     { 
      array1[i] = a; 
     } 

    } 
} 
+0

你只要求输入一次。 – Carcigenicate

+0

它应该是'array1 [i] = sc.nextInt();' – Saravana

回答

1

变化

for (int i = 0; i < 9; i++) 
    { 
     int a = sc.nextInt(); 
     array1[i] = a; 
    } 

甚至

for (int i = 0; i < 9; i++) 
    { 
     array1[i] = sc.nextInt(); 
    } 
+0

谢谢!我看到出了什么问题 –

0

这很简单,你可以扫描对象的输入值只分配给数组的索引:

import java.util.Scanner; 

public class AssignSeven 
{ 
    public static void main(String[] args) 
    { 
     int [] array1 = new int[10]; 
     System.out.println("Enter 10 numbers"); 
     Scanner sc = new Scanner(System.in); 

     // Where you had the original input 
     // int a = sc.nextInt(); 

     for (int i = 0; i < 9; i++) 
     { 
      // Instead of array1[i] = a; you have 
      array1[i] = sc.nextInt(); 
     } 

    } 
} 

希望这有助于!

+0

谢谢!我明白了什么是错的! –