【问题标题】:Printing in Order and Reverse Order顺序打印和倒序打印
【发布时间】:2013-11-15 19:53:50
【问题描述】:

下面的代码需要将数组中的随机数依次和倒序打印出来。我有逆序打印,但由于某种原因,我似乎无法让它以原始顺序打印。我不确定要修改什么。我目前所打印的“按顺序”打印出一个空白行,“反向顺序”打印出数字。我需要解决什么问题?谢谢!

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] + "  ");
   }
}

【问题讨论】:

  • 检查for (int index = numbers.length+1; index &lt;= 10; index++) 的边界。提示:检查程序中的第一个 for 循环。

标签: java arrays loops random


【解决方案1】:

下面的代码:

for (int index = numbers.length+1; index <= 10; index++)

这里index的初始化值为number.length+1,这个值为10+1=11大于 10,因此在这个 for 循环中永远不会满足条件。这就是数组没有按顺序打印的原因。

您需要进行如下更改:

发件人

for (int index = numbers.length+1; index <= 10; index++)
      System.out.print (numbers[index] + "  ");

for (int index = 0; index <10; index++)
        System.out.print(numbers[index] + "  ");

修改后的代码如下:

公共类 RandomPrintOut { //------------------------------------------------ ----------------- // //------------------------------------------------ ----------------- 公共静态无效主要(字符串 [] 参数) {

   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] + "  ");

} }

在控制台中打印的结果输出:

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  

【讨论】:

  • 为什么要做这样的改变?前者和后者使用有什么区别?
  • 哇哦。我有,但我有 index
  • 在这样的 for 循环中 for (int index = numbers.length+1; index
  • @Lou44 区别主要在于起始索引。此外,不要像这里所做的那样对循环范围进行硬编码。使用index &lt; numbers.length
【解决方案2】:

仔细查看此循环中index 的值

  for (int index = numbers.length+1; index <= 10; index++)
      System.out.print (numbers[index] + "  ");

尝试手动操作,看看效果如何。这应该可以帮助您识别问题。

【讨论】:

    【解决方案3】:
    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] + "  ");
          }
       }
    }
    

    【讨论】:

    • 来吧,不要硬编码10。此外,如果 OP 只能复制粘贴,他/她将不会学习。
    • 好,继续只给 OP 代码以使其工作,而不对问题和解决方案进行任何解释,肯定 OP 会自己学习并在遇到相同问题时停止询问这个问题未来(可能在几天后)。
    • @Zong 和 Luiggi:同意。但是觉得解释起来太简单了,他/她看了代码就明白了……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多