集册 Java实例教程 从控制台读取Char

从控制台读取Char

欢马劈雪     最近更新时间:2020-01-02 10:19:05

651
从控制台读取Char
//from n o w j a   v  a . c o m - 时  代  Java


//package com.nowjava;


public class Main {

    public static void main(String[] argv) throws Exception {

        String prompt = "nowjava.com";

        System.out.println(inChar(prompt));

    }


    public static char inChar(String prompt) {

        int aChar = 0;


        inputFlush();

        printPrompt(prompt);


        try {

            aChar = System.in.read();

        } catch (java.io.IOException e) {

            System.out.println("Input error");

        }

        inputFlush();
        /*来自 
         时 代 J a v a - nowjava.com*/

        return (char) aChar;

    }


    public static void inputFlush() {

        int dummy;

        int bAvail;


        try {

            while ((System.in.available()) != 0)

                dummy = System.in.read();

        } catch (java.io.IOException e) {

            System.out.println(
展开阅读全文