reversearray1.java

来自「Java 入门书的源码」· Java 代码 · 共 35 行

JAVA
35
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/*  Reverses an array.  The user inputs the
 *  size of the array and enters its components.
 */

import iopack.Io;         
public class ReverseArray1 {
  public static void main(String [] args)  {
    int size = Io.readInt("Enter the array size");
    int [] score = new int[size];
    int temp;                     // used to store a value during a swap
    int left = 0;                 // index of the left element to swap
    int right = score.length -1;  // index of the right element to swap

    for (int i=0; i<size; i++)
      score[i] = Io.readInt("Enter score["+i+"]");  
    while (left < right) {
      temp           = score[left];
      score[left]    = score[right];
      score[right]   = temp;
      right--;
      left++;
    }
    System.out.print("The reversed array is {");
    for (int i=0; i<score.length; i++) {
      if (i!=0) System.out.print(",");
      System.out.print(score[i]);
    }
    System.out.println("}");
    Io.readString("Press any key to exit");   // Added for IDE use
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?