java中用键盘输入数据
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.io.*;
public class HelloAl {
public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(reader); System.out.print("Enter your name: ");
String name = input.readLine();
System.out.println("Hello, " + name + "!");
}
}
我想继续问一下:
如果我想输入的字符是int型的.
也就是说我想read 出来的还是int型,我应该怎么办>
我记得BufferedReader类.没有readInt的方法吧!
是不是可以用别的类实现输入!
还是需要再调用一个stringToInt的方法呀!
import java.io.*;
public class YearOfBirth {
public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(reader); System.out.print("Enter your age: ");
String text = input.readLine();
int age = new Integer(text).intValue();
System.out.println("You are " + age + " years old, now,");
int year = 2003 - age;
System.out.println("so you were probably born in " + year);
}
}
定一一个int型数组,怎样从键盘输入为数组赋值?
int[] a = new int[10];
String inform;
BufferedReader bf = new BufferedReader( newInputStreamReader(System.in)
);
for(int i=0;i<10;i++)
{
inform = bf.readLine();
a[i] = count + Integer.parseFloat(inform);
}
importjava.util.Scanner;
public class CountNumberDemo {
public static void main(String[] args) {
int sum = 0;
int [] arr = new int [10];
Scanner s = new Scanner(System.in);
for(int i= 0; i<10;i++){
System.out.println("第" + (i+1) + "个数字:");
arr[i] = s.nextInt();
sum += arr[i];
}
System.out.println("你输入的数字是:");
for(int a : arr){
System.out.print(a + "\t");
}
System.out.println("\n他们的和是:" + sum);
}
}