console.java

来自「Java调用Windows API,支持Office」· Java 代码 · 共 93 行

JAVA
93
字号
//******************************************************************
// Released under the DevelopMentor OpenSource Software License.
// Please consult the LICENSE file in the project root directory,
// or at http://www.develop.com for details before using this
// software.
//******************************************************************

package org.jawin.donated.win32.io;

import java.io.*;

/**
 * Simple wrapper class for console input/output
 */
public class Console 
{
  /**
   * Wraps System.in for reading in a line at time
   */
  static BufferedReader input = new BufferedReader(new InputStreamReader((System.in)));

  /**
   * Prevents construction
   */
  private Console() 
  {
  }

  /** 
   * Prints <code>prompt</code> and returns user's response 
   */
  public static String prompt(String prompt) 
  {
    System.out.println(prompt);
	try 
	{
		return input.readLine();
	}
	catch (IOException ioe) 
	{
		return null;
	}
  }

  /**
   * Prints <code>prompt</code> and forces user to enter 
   * <code>choice</code> before continuing
   */
  public static void acceptChoice(String prompt, String choice) 
  {
    System.out.println(prompt);
    while (true) 
      {
	try 
	  {
	    if(choice.equals(input.readLine()))
	      break;	
	  }
	catch (IOException ioe) 
	  {
	  }
      }
  }

  /**
   * Prints <code>prompt</code> and forces user to enter 
   * one of <code>choices</code> before continuing
   */
  public static String acceptChoice(String prompt, String[] choices) 
  {
    System.out.println(prompt);
    while (true) 
      {
	try 
	  {
	    String response = input.readLine();
	    for (int n=0; n<choices.length; n++) 
	      {
		if (choices[n].equals(response)) 
		  {
		    return response;
		  }
	      }
	  }
	catch (IOException ioe) 
	  {
	  }
      }	
  }


}

⌨️ 快捷键说明

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