reverser.java

来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 43 行

JAVA
43
字号
import java.io.*;
import javagently.*;
import java.util.Stack;

public class Reverser {

  /* Testing the stack class  by J M Bishop  Jan 1997
   * -----------------------  Java 1.1
   *                          updated August 2000
   * Reads a sentence and reverses it using a stack.
   *Illustrates push, pop and empty in Java's Stack class.
   */

  public static void main (String args [])
    throws IOException {
    new Reverser();
  }

  Reverser () throws IOException {

    Stream in = new Stream (System.in);

    Stack S = new Stack();
    System.out.println("**** Testing the Stack class ****");
    System.out.println("Type in a sentence and end the input " +
       "(cntrl-D or cntrl-Z)");
    System.out.println("The original sentence is: ");

    while (true) {
      try {
        String word = in.readString();
        S.push(word);
      }
      catch (EOFException e) {break;}
    }

    System.out.println("The reversed sentence is:");
    while (!S.empty())
      System.out.print (S.pop()+" ");
    System.out.println();
  }
}

⌨️ 快捷键说明

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