Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取整数列表并使用数组反向显示它们_Java_Arrays - Fatal编程技术网

Java 获取整数列表并使用数组反向显示它们

Java 获取整数列表并使用数组反向显示它们,java,arrays,Java,Arrays,如果我的输入是1 2 3,输出也是1 2 3,我如何使这些数字显示3 2 1 public static void main(String[] args) { // TODO code application logic here Scanner s = new Scanner(System.in); String text = s.nextLine(); String[] entries = text.split(" "); int[] nums

如果我的输入是1 2 3,输出也是1 2 3,我如何使这些数字显示3 2 1

 public static void main(String[] args) {
    // TODO code application logic here
    Scanner s = new Scanner(System.in);

    String text = s.nextLine();


    String[] entries = text.split(" ");
    int[] nums = new int[entries.length];

     for(int i = 0; i < entries.length; i++){
        nums[i] = Integer.parseInt(entries[i]);
    }
     for(int i = 0; i < entries.length; i++){
        System.out.println(nums[i]);
    }
}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
扫描仪s=新的扫描仪(System.in);
字符串text=s.nextLine();
字符串[]项=text.split(“”);
int[]nums=新的int[entries.length];
for(int i=0;i

}

您可以向后循环您的
条目
数组。这将涉及以
条目长度开始
int i
减去1(因为这是数组中的最后一个索引-即最后一个数字)。它还要求您在
i>=0
时保持循环。最后,您需要减少变量,而不是增加变量
i
。这样,计数器
i
将从循环的末尾到数组的开头(例如:如果输入“1 2 3”,则
i
将从索引:2、1、0开始)

见下例:

public static void main(String[] args) {
   // TODO code application logic here
   Scanner s = new Scanner(System.in);

   String text = s.nextLine();


   String[] entries = text.split(" ");
   int[] nums = new int[entries.length];

   for(int i = 0; i < entries.length; i++) {
     nums[i] = Integer.parseInt(entries[i]);
   }
   for(int i = entries.length-1; i >= 0; i--) {
     System.out.println(nums[i]);
   }
}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
扫描仪s=新的扫描仪(System.in);
字符串text=s.nextLine();
字符串[]项=text.split(“”);
int[]nums=新的int[entries.length];
for(int i=0;i=0;i--){
System.out.println(nums[i]);
}
}

如果要按相反顺序存储数字:

for(int i = 0; i < entries.length; i++)
{
     nums[i] = Integer.parseInt(entries[entries.length-i-1]); 
} 
请尝试以下代码:

public static void main(String[] args) 
{
  Scanner s = new Scanner(System.in);
  String text = s.nextLine();
  String[] entries = text.split(" ");
  for(int i = entries.length-1; i >= 0; i--) 
  {
     System.out.print(Integer.parseInt(entries[i])+ " ");
  }
}

如果您想要Java8版本,下面是代码

Scanner s = new Scanner(System.in);      
String text = s.nextLine(); 
String[] entries = text.split("\\s");

List<Integer> integers = Arrays.stream(entries)
        .map(Integer::valueOf)
        .collect(Collectors.toList());

Collections.reverse(integers);

integers.forEach(integer -> System.out.print(String.format("%d ", integer)));
Scanner s=新的扫描仪(System.in);
字符串text=s.nextLine();
String[]entries=text.split(\\s”);
列表整数=数组.stream(条目)
.map(整数::valueOf)
.collect(Collectors.toList());
集合。反向(整数);
integers.forEach(integer->System.out.print(String.format(“%d”,integer));

\\s
表示“空白”,我建议您在最后关闭
扫描仪

如果您的唯一意图是反转输入,那么您为什么不能反转
字符串文本呢?@CommonMan是的,如果是这样,如果您的唯一意图是反转输入,那么您为什么不能反转
字符串文本呢?
Scanner s = new Scanner(System.in);      
String text = s.nextLine(); 
String[] entries = text.split("\\s");

List<Integer> integers = Arrays.stream(entries)
        .map(Integer::valueOf)
        .collect(Collectors.toList());

Collections.reverse(integers);

integers.forEach(integer -> System.out.print(String.format("%d ", integer)));