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

从控制台读取双声

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

480
从控制台读取双声


//package com.nowjava;/* 来自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m*/


public class Main {

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

        String prompt = "nowjava.com";

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

    }


    public static double inDouble(String prompt) {

        while (true) {

            inputFlush();

            printPrompt(prompt);

            try {

                return Double.valueOf(inString().trim());

            } catch (NumberFormatException e) {

                System.out

                        .println("Invalid input. Not a floating point number");

            }
            /**
             from
            * N o  w  J a v a . c o m - 时  代  Java 
            **/

        }

    }


    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("Input error");

        }

    }


    public static void printPrompt(String prompt) {

        System.out.print(prompt + " ");

        System.out.flush();

    }


    public static String inString(String prompt) {

        inputFlush();

        printPrompt(prompt);

        return inString();

    }


    public static String inString() {

        int aChar;

        String s = "";

        boolean finished = false;


        while (!finished) {

            try {

                aChar = System.in.read();

                if (aChar < 0 || (char) aChar == '\n')

                    finished = true;

                
展开阅读全文