Java 数组未正确输出

Java 数组未正确输出,java,arrays,Java,Arrays,在试图创建一个向后显示一组输入数组的程序时,我遇到了一个问题,即无论我做什么,所有变量都输出为“0”。这可能与我用来设置数组的循环有一些简单的问题,我忽略了一些东西,我只需要一双新的眼睛来告诉我我做错了什么 import java.io.*; class ReverseArray { public static void main (String args[]) throws IOException { InputStreamReader inStream = n

在试图创建一个向后显示一组输入数组的程序时,我遇到了一个问题,即无论我做什么,所有变量都输出为“0”。这可能与我用来设置数组的循环有一些简单的问题,我忽略了一些东西,我只需要一双新的眼睛来告诉我我做错了什么

import java.io.*;
class ReverseArray
{
    public static void main (String args[]) throws IOException
    {
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(inStream);
        String inInput;
        int count = 0;
        int[] userInput = new int[10];
        int newInput = 0;


        System.out.println ("Please enter ten whole numbers:");

        for (count = 0; count <= 9; count++) 
        {
            inInput = reader.readLine();
            newInput = Integer.parseInt(inInput);
            newInput = userInput[count];
        }

        System.out.println ("These numbers in reverse order is:");
        System.out.println (userInput[9]);
        System.out.println (userInput[8]);
        System.out.println (userInput[7]);
        System.out.println (userInput[6]);
        System.out.println (userInput[5]);
        System.out.println (userInput[4]);
        System.out.println (userInput[3]);
        System.out.println (userInput[2]);
        System.out.println (userInput[1]);
        System.out.println (userInput[0]);

    }
}
import java.io.*;
类反向通道
{
公共静态void main(字符串args[])引发IOException
{
InputStreamReader inStream=新的InputStreamReader(System.in);
BufferedReader读取器=新的BufferedReader(流内);
字符串输入;
整数计数=0;
int[]用户输入=新的int[10];
int newInput=0;
System.out.println(“请输入十个整数:”);
对于(count=0;count我想你想要

userInput[count] = newInput;
不是

改为:

userInput[count] = newInput;
或者,对于更多上下文,您的分配循环:

for (count = 0; count <= 9; count++) {
    inInput = reader.readLine();
    newInput = Integer.parseInt(inInput); 
    newInput = userInput[count]; 
} 

for(count=0;count
newInput=userInput[count];

需要

userInput[count]=newInput;

但是你可以跳过不必要的作业


userInput[count]=Integer.parseInt(inInput);
语句
newInput=userInput[count];
将语句右侧的值,即索引
count
处的数组元素,分配给变量
newInput
。要执行相反的操作:

userInput[count] = newInput;   // assign the value of newInput to array element

在你的电话线上


newInput=userInput[count];

这两个变量应该是相反的


userInput[count]=newInput;

这是因为您需要反转此行:

userInput[count] = newInput;
但是,使用
for循环
打印结果,使用
长度
计算数组长度:

import java.io.*;

class ReverseArray
{
    public static void main (String args[]) throws IOException
    {
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(inStream);
        int[] userInput = new int[10];

        // fill the array
        System.out.println ("Please enter ten whole numbers:");
        for (int count = 0; count < userInput.length; count++)
            userInput[count] = Integer.parseInt(reader.readLine());

        // print out the array values
        System.out.println ("These numbers in reverse order is:");
        for (int count = userInput.length - 1; count >= 0; count--)
            System.out.println (userInput[count]);
    }
}
import java.io.*;
类反向通道
{
公共静态void main(字符串args[])引发IOException
{
InputStreamReader inStream=新的InputStreamReader(System.in);
BufferedReader读取器=新的BufferedReader(流内);
int[]用户输入=新的int[10];
//填充数组
System.out.println(“请输入十个整数:”);
对于(int count=0;count=0;count--)
System.out.println(用户输入[count]);
}
}

因为您尚未为数组指定任何值


newInput=userInput[count];

将此更改为

userInput[count]=newInput;

userInput[count] = newInput;   // assign the value of newInput to array element
userInput[count] = newInput;
import java.io.*;

class ReverseArray
{
    public static void main (String args[]) throws IOException
    {
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(inStream);
        int[] userInput = new int[10];

        // fill the array
        System.out.println ("Please enter ten whole numbers:");
        for (int count = 0; count < userInput.length; count++)
            userInput[count] = Integer.parseInt(reader.readLine());

        // print out the array values
        System.out.println ("These numbers in reverse order is:");
        for (int count = userInput.length - 1; count >= 0; count--)
            System.out.println (userInput[count]);
    }
}