集册 Java实例教程 接受键盘输入

接受键盘输入

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

511
使用java.io.BufferedReader和java.io.InputStreamReader类读取键盘输入并将其存储到局部变量中。

import java.io.*; /** 来 自 nowjava.com - 时代Java**/


public class Main { 

    public static void main(String[] args){ 

        BufferedReader readIn = new BufferedReader( new InputStreamReader(System.in)); 

        String numberAsString = ""; 

        long numberAsLong = 0; 


        boolean numberIsValid = false; 

        do { 

            /* Ask the user for a number. */ 

            System.out.println("Please enter a number: "); 

            try { 

                numberAsString = readIn.readLine(); 

                System.out.println("You entered " + numberAsString); 

            } catch (IOException ex){ 

                System.out.println(ex); 

            } 
            /* 
             来自 
            *时 代 J a v a*/


            /* Convert the number into binary form. */ 

            try { 

                numberAsLong = Long.parseLong(numberAsString); 

                numberIsValid = 
展开阅读全文