Java 按顺序和逆序打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20009479/
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
Printing in Order and Reverse Order
提问by Lou44
The following code needs to print the random numbers in the array in order and reverse order. I have the reverse order printing but for some reason I can't seem to get it to print in original order. I'm not sure what to modify. What I currently have prints out a blank line for the "in order" and the numbers for "reverse order". What do I need to fix? Thank you!
下面的代码需要按顺序和逆序打印数组中的随机数。我有逆序打印,但由于某种原因,我似乎无法按原始顺序打印。我不确定要修改什么。我目前所拥有的打印出“按顺序”的空行和“逆序”的数字。我需要修复什么?谢谢!
public class RandomPrintOut
{
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
public static void main (String[] args)
{
int numbers[] = new int [10];
for (int i = 0 ; i < 10 ; i++)
{
numbers [i] = (int) (Math.random () * 100);
}
System.out.println ("The size of the array: " + numbers.length);
System.out.println ("The numbers in order:");
for (int index = numbers.length+1; index <= 10; index++)
System.out.print (numbers[index] + " ");
System.out.println ("\nThe numbers in reverse order:");
for (int index = numbers.length-1; index >= 0; index--)
System.out.print (numbers[index] + " ");
}
}
采纳答案by MouseLearnJava
For the code below:
对于下面的代码:
for (int index = numbers.length+1; index <= 10; index++)
Here the initialized value for index
is number.length+1, this value is 10+1=11
. This is greater than 10, as a result, the codition is never satisfied in this for-loop. That's why the array is not printed in order.
这里的初始值index
是number.length + 1,此值10+1=11
。 这大于 10,因此在此 for 循环中永远不会满足条件。这就是数组没有按顺序打印的原因。
You need to do the change as follows:
您需要按如下方式进行更改:
Frome
弗罗姆
for (int index = numbers.length+1; index <= 10; index++)
System.out.print (numbers[index] + " ");
to
到
for (int index = 0; index <10; index++)
System.out.print(numbers[index] + " ");
Code after change is as follows:
更改后的代码如下:
public class RandomPrintOut { //----------------------------------------------------------------- // //----------------------------------------------------------------- public static void main (String[] args) {
公共类 RandomPrintOut { //-------------------------------------------- --------------------- // //------------------------- ---------------------------------------- public static void main (String[] args) {
int numbers[] = new int [10];
for (int i = 0 ; i < 10 ; i++)
{
numbers [i] = (int) (Math.random () * 100);
}
System.out.println ("The size of the array: " + numbers.length);
System.out.println ("The numbers in order:");
for (int index = 0; index < 10; index++)
System.out.print (numbers[index] + " ");
System.out.println ("\nThe numbers in reverse order:");
for (int index = numbers.length-1; index >= 0; index--)
System.out.print (numbers[index] + " ");
} }
} }
An result output printed in Console:
控制台中打印的结果输出:
The size of the array: 10
The numbers in order:
31 50 49 99 29 54 41 16 7 21
The numbers in reverse order:
21 7 16 41 54 29 99 49 50 31
回答by Tyler Lee
Take a closer look at the values of index
over this loop
仔细看看index
这个循环上的值
for (int index = numbers.length+1; index <= 10; index++)
System.out.print (numbers[index] + " ");
Try doing it by hand and see what it looks like. That should help you identify the problem.
尝试手工制作,看看它是什么样子。这应该可以帮助您确定问题。
回答by tumchaaditya
public class RandomPrintOut {
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
public static void main (String[] args) {
int numbers[] = new int [10];
for (int i = 0 ; i < 10 ; i++) {
numbers [i] = (int) (Math.random () * 100);
}
System.out.println ("The size of the array: " + numbers.length);
System.out.println ("The numbers in order:");
for (int index = 0; index <= numbers.length-1; index++) {
System.out.print (numbers[index] + " ");
}
System.out.println ("\nThe numbers in reverse order:");
for (int index = numbers.length-1; index >= 0; index--) {
System.out.print (numbers[index] + " ");
}
}
}