Topic: 在java中用什么语句做到从键盘输入数据

  Print this page

1.在java中用什么语句做到从键盘输入数据 Copy to clipboard
Posted by: genius
Posted on: 2003-12-15 23:37

就想C语言中的scanf一样。谢谢了,我真的是第一次接触JAVA,什么都不懂!!!

2.Re:在java中用什么语句做到从键盘输入数据 [Re: genius] Copy to clipboard
Posted by: nothing
Posted on: 2003-12-16 00:02

// Programming with Java by John R. Hubbard
// Copyright McGraw-Hill, 1999
// Example 1.2 on page 14

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 + "!");
}
}

3.Re:在java中用什么语句做到从键盘输入数据 [Re: genius] Copy to clipboard
Posted by: yeehya
Posted on: 2003-12-24 15:38

我想继续问一下:
如果我想输入的字符是int 型的.
也就是说我想read 出来的还是int 型,我应该怎么办>
我记得BufferedReader 类.没有readInt的方法吧!
是不是可以用别的类实现输入!
还是需要再调用一个stringToInt的方法呀!
请执教, 先谢谢!

4.Re:在java中用什么语句做到从键盘输入数据 [Re: yeehya] Copy to clipboard
Posted by: nothing
Posted on: 2003-12-24 15:45

// Programming with Java by John R. Hubbard
// Copyright McGraw-Hill, 1999
// Example 1.3 on page 17

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);
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923