java 需要帮助以逆序书写数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15939954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:20:54  来源:igfitidea点击:

Need help writing numbers in the Reverse ORDER

java

提问by user2268587

I need some help with this assignment I've been given. Not asking anyone to do my work but I'm really honestly stuck on how to even do this.

我需要一些帮助来完成这项任务。不要求任何人做我的工作,但老实说,我真的被困在如何做到这一点上。

I'm supposed to write a program that prompts the user to enter 10 numbers and then have it write all the numbers in reverse order.

我应该编写一个程序,提示用户输入 10 个数字,然后让它以相反的顺序写出所有数字。

Example: Enter 10 Numbers: 23 89 21 55 67 89 99 13 98 78 Reverse Order: 78 98 13 99 89 67 55 21 89 23

示例:输入 10 个数字:23 89 21 55 67 89 99 13 98 78 逆序:78 98 13 99 89 67 55 21 89 23

So far all I have is how to get the user inputs. If anyone can push me in the right direction, i'd be very grateful!

到目前为止,我所拥有的只是如何获取用户输入。如果有人能将我推向正确的方向,我将不胜感激!

import java.util.*;

public class ReverseNumbers
{
public static void main(String[] args)
  {

    Scanner keyboard = new Scanner(System.in);

    int[] values;
    values = new int[10];

    //Ask the user to enter 10 integers
    System.out.println("Please enter 10 numbers:");
    for (int i = 0; i< values.length; i++) 
    {
      values[i] = keyboard.nextInt();
    }
    int[] reverseNums;
    reverseNums = new int[10];
    for (int i = (values.length -1); i>= 0; i--) 
    {
      reverseNums[ reverseNums.length -1 -i ] = values[ i ];
      System.out.println( reverseNums[ i ] );
    } 
 }
}

回答by Abi

If you dont want to store the reversed values

如果您不想存储反转的值

    for (int i = (values.length -1); i>= 0; i--) 
    {       
      System.out.println( values[ i ] );
    } 

回答by tboyce12

Your code looks good for reading inputs into values. Now you can loop over that array in reverse and print the values:

您的代码看起来很适合将输入读入values. 现在您可以反向循环该数组并打印值:

for (int i = 0; i < values.length; i++)
  System.out.println(values[values.length - i - 1]);

Think about it, when i == 0it will print values[9]since 10 - 0 - 1 = 9. At the end, when i == 9it will print values[0]since 10 - 9 - 1 = 0.

想一想,什么时候i == 0打印,values[9]因为 10 - 0 - 1 = 9。最后,什么i == 9时候打印,values[0]因为 10 - 9 - 1 = 0。

As you can see, there is no need for the extra reverseNumsarray.

如您所见,不需要额外的reverseNums数组。

PS: If you want, you can combine int[] values;and values = new int[10];into a single line: int[] values = new int[10];

PS:如果需要,可以将int[] values;values = new int[10];合并为一行:int[] values = new int[10];

回答by Adriaan Koster

I think your code is creating the array correctly. You are just printing out the numbers in the original order because your second for-loop is iterating over them backwards. You can see the correct result by adding the statement below after the last loop:

我认为您的代码正确地创建了数组。您只是按原始顺序打印出数字,因为您的第二个 for 循环正在向后迭代它们。通过在最后一个循环后添加以下语句,您可以看到正确的结果:

System.out.println(java.util.Arrays.toString(reverseNums));

You can also perform the complete task with only one iteration:

您还可以仅通过一次迭代来执行完整的任务:

Scanner keyboard = new Scanner(System.in);

int[] reverseNums= new int[10];
System.out.println("Please enter 10 numbers:");
for (int i = 0; i< values.length; i++) {
  reverseNums[values.length -1 - i] = keyboard.nextInt();
}
System.out.println(java.util.Arrays.toString(reverseNums));

回答by Kyle Saltzberg

Read in the whole line as a string using Scanner.nextLine(), and then split it into an array using String.split(" ");. After this, you can simply iterate from the end of the array going backwards and print the numbers.

使用 将整行作为字符串读取Scanner.nextLine(),然后使用 将其拆分为数组String.split(" ");。在此之后,您可以简单地从数组的末尾向后迭代并打印数字。

回答by karthick

If it's not an assignment then why not try using http://commons.apache.org/proper/commons-lang/library

如果这不是作业,那么为什么不尝试使用http://commons.apache.org/proper/commons-lang/

ArrayUtils.reverse(int[] array)

回答by Bohemian

To reverse it:

要反转它:

for (int i = 0; i < values.length; i++)
    reverseNums[values.length - i - 1] = values[i];