我需要键入3个整数并将它们存储在一个数组中

时间:2013-12-11 02:23:16

标签: java

我是Java的新手,但我在使用Visual Basic进行编程方面有一些经验。这就是我到目前为止所做的:

class Assignment11 
{
    public static void main (String[] args) {
        int array[] = new int[] {0,0,0};
        System.out.println("Input first number: ");
        array[0]= scan.nextInt();
        System.out.println("Input second number: ");
        array[1]= scan.nextInt();
        System.out.println("Input third number: ");
        array[2]= scan.nextInt();
        System.out.println(array[0]);
    }
}

我希望程序说输入第一个数字,然后读取键入的值并将其存储在数组中。这样做的代码是什么?

2 个答案:

答案 0 :(得分:0)

只需创建一个扫描仪......

import java.util.Scanner;
Scanner scan = new Scanner(System.in);

int array[] = new int[3];
System.out.println("Input first number: ");
array[0]= scan.nextInt();
System.out.println("Input second number: ");
array[1]= scan.nextInt();
System.out.println("Input third number: ");
array[2]= scan.nextInt();
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);

答案 1 :(得分:0)

试试这个

    class Assignment11 {
    public static void main (String[] args) {
        int array[] = new int[] {0,0,0};
        Scanner scan = new Scanner(System.in);//this will wait for user input
        System.out.println("Input first number: ");//user will input the first num
        array[0]= scan.nextInt();//scanner will read the input and put into array
        System.out.println("Input second number: ");
        array[1]= scan.nextInt();
        System.out.println("Input third number: ");
        array[2]= scan.nextInt();
        System.out.println(array[0]);
    }
   }