java中从键盘输入一整数的问题

来源:百度知道 编辑:UC知道 时间:2024/06/03 06:38:17
import java.io.*;
public class QqApp{

public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please insert a integer");
int a = Integer.parseInt(br.readLine());
System.out.println("You input :"+a);
}
}
这里的throws IOException什么意思
只有这一种方法么?
我要简单点的

throws IOException 是抛出异常,new Bufferedreader时会产生IOException异常,用throws IOException可将这个异常抛给类QqApp的父类,它本身不会处理这个异常。更常用的方法是用try-catch自己处理异常。
从键盘输入整数是I/O流的内容,有很多种实现方法,除了标准输入流System.in外,常用的还有Scanner等

import java.util.*;//Scanner位于util包
public class HelloFriend
{
String name;
int age;
HelloFriend()
{
Scanner in = new Scanner(System.in);
//输入字符
System.out.println("What's your name?");
name = in.nextLine();
//输入整形数据
System.out.println("How old are U?");
age = in.nextInt();
}
void display()
{
System.out.println("Hello, "+name+". Next year U'll be "+(age+1));
}
}

public class InputTest
{
public static void main(String[] args)
{
HelloFriend aFriend = new HelloFriend();
aFriend.display();
}
}
看了这个你就明白了