数组打印Java [英] Array Printing Java

查看:108
本文介绍了数组打印Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我希望我的代码使用户能够输入x个整数(他们选择了x),然后他们会将每个输入的整数值存储在我的数组中.

Basically I want my code to enable the user to enter x amount of integers (they chose x), then they will then store each of their inputted integer values in my array.

由于某些原因,在输入所有值后打印出数组:

For some reason when I print my array out after all values have been entered:

我明白了:

代码:

    Scanner input = new Scanner(System.in);
    System.out.print("How many integers will you enter:");
    int amount = input.nextInt();

    int myArray[] = new int[amount];

    for (int counter = 0; counter < amount; counter ++){
        myArray[counter] = input.nextInt();
    }
    System.out.println(myArray);

控制台:

        How many integers will you enter:2
        4
        2
       [I@55f96302

推荐答案

好的,您可以取消for循环并以这种方式完成

Ok, you can take away the for loop and do it this way

System.out.println(Arrays.toString(myArray));

我强烈建议您使用for循环.每次循环运行时,它每次输出一个元素,而对于for循环,它遍历每个元素.您需要将println更改为此(在循环内部)

I would strongly recommend to do it with the for loop. It prints one element at the time every time the loop runs, and with your for loop it runs through every element. You need to change the println to this (inside you for loop)

System.out.println(myArray[counter]);

或者如果您想将数组放在同一行

or if you want to have the array on the same line

System.out.print(myArray[counter]+ " ");

稍后在进行编程时,您将看到它与Arrays.toString()方法相比有多有用.

Later on when programming you are going to see how useful this is compared to Arrays.toString() method.

这篇关于数组打印Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆