赞
踩
Java开发控制台程序的输入输出:
一.使用Scanner获取用户从控制台的输入:
- package Com.icoem;
-
- import java.util.Scanner;
-
- public class HelloWord {
- public static void main(String[] args) {
- try{
- System.out.print("请输入分数:");
- Scanner input=new Scanner(System.in);
- int fraction=input.nextInt();
- System.out.println("加分前的分数"+String.valueOf(fraction));
- int temp=0;
- while(fraction<60){
- temp++;
- fraction++;
- }
- System.out.println("加分后的分数:"+String.valueOf(fraction));
- System.out.println("加分次数:"+String.valueOf(temp));
- }catch (Exception e) {
- System.out.println(e.getCause().getMessage());
- }
- }
- }
二使用BufferedReader
- package Com.icoem;
-
-
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
-
- public class HelloWord {
- public static void main(String[] args) {
- BufferedReader intput=new BufferedReader(new InputStreamReader(System.in));
- try{
- System.out.print("请输入分数:");
- int fraction=Integer.parseInt(intput.readLine()) ;
- System.out.println("加分前的分数"+String.valueOf(fraction));
- int temp=0;
- while(fraction<60){
- temp++;
- fraction++;
- }
- System.out.println("加分后的分数:"+String.valueOf(fraction));
- System.out.println("加分次数:"+String.valueOf(temp));
- }catch (Exception e) {
- System.out.println(e.getCause().getMessage());
- }
- }
- }
BufferedReader类用于缓冲读取字符,将字节流封装成BufferedReader对象,然后用readLine()逐行读入字符流,直到遇到换行符为止(相当于反复调用Reader类对象的read()方法读入多个字符)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。