java中没有和C/C++对应的输入函数。要解决输入问题,有两个办法:最简单的是利用其swing.JOptionPane对话框类,接收字符串输入,然后利用Integer/Double.parseXX()方法将其转换成相应的类型。另一种方法是自己写一个输入类,解决从命令行输入的问题。
我这里有一段代码可供参考,但这决不是唯一的方法。import java.io.*;public class FormattedInput
{
private int ttype;
private StreamTokenizer token = new StreamTokenizer( new BufferedReader(
new InputStreamReader(System.in)));
private int readToken()
{
try
{
ttype = token.nextToken();
return ttype;
}
catch (IOException e)
{
e.printStackTrace();
}
return 0;
}
public int readInt() throws InvalidUserInputException
{
if (readToken() != token.TT_NUMBER)
{
throw new InvalidUserInputException("readInt() failed. Input data not numeric");
}
return (int)token.nval;
}
public double readDouble()
{
//if (readToken() == token.TT_NUMBER)
return 0;
}
public static void main(String[] args)
{
FormattedInput kb = new FormattedInput();
try {
System.out.print("Enter an integer: ");
System.out.println("Integer read: "+kb.readInt());
} catch (InvalidUserInputException e) {
System.out.println("not numeric");
e.getMessage();
}
}
}public class InvalidUserInputException extends Exception
{
public InvalidUserInputException(String msg)
{
super(msg);
}
}
我这里有一段代码可供参考,但这决不是唯一的方法。import java.io.*;public class FormattedInput
{
private int ttype;
private StreamTokenizer token = new StreamTokenizer( new BufferedReader(
new InputStreamReader(System.in)));
private int readToken()
{
try
{
ttype = token.nextToken();
return ttype;
}
catch (IOException e)
{
e.printStackTrace();
}
return 0;
}
public int readInt() throws InvalidUserInputException
{
if (readToken() != token.TT_NUMBER)
{
throw new InvalidUserInputException("readInt() failed. Input data not numeric");
}
return (int)token.nval;
}
public double readDouble()
{
//if (readToken() == token.TT_NUMBER)
return 0;
}
public static void main(String[] args)
{
FormattedInput kb = new FormattedInput();
try {
System.out.print("Enter an integer: ");
System.out.println("Integer read: "+kb.readInt());
} catch (InvalidUserInputException e) {
System.out.println("not numeric");
e.getMessage();
}
}
}public class InvalidUserInputException extends Exception
{
public InvalidUserInputException(String msg)
{
super(msg);
}
}
2.int i=System.in.read();
Which one?()
D:\MyJava>javac formattedinput.java
formattedinput.java:51: class InvalidUserInputException is public, should be dec
lared in a file named InvalidUserInputException.java
public class InvalidUserInputException extends Exception为什么不删掉类InvalidUserInputException前的public就会出这样的错误呢?你们是不是也这样?我以前的其它程序也是,有些类前如果用了public就会出错.
另:关于编译错误,是因为你把class InvalidUserInputException和其他class放在一个文件里了:java只允许一个文件里有一个public class。因此,你要么把class InvalidUserInputException单独作为一个文件编译,文件名一定要和class名一致(包括大小写);要么取消public关键字,这样才能和其他class放在同一文件中编译。
不知我解释清楚了没有。
还有点问题,把class InvalidUserInputException单独作为一个文件编译之后的文件如果和main()在一个目录,应该可以直接用吧,那如果不在同一个目录呢?是不是有个什么import?我是刚学,用的清华张孝祥老师的书,很适合初学者,但很多地方讲得不深。
import yourFolderName.subFolderName.className;
package myimported;public class MyImportedClass
{
public static void message()
{
System.out.println("Hello");
}
}注意:一定要在D:\myjava下编译该文件,不要在D:\myjava\myimported下编译;编译格式为
javac myimported/MyImportedClass.java 或javac myimported.MyImportedClass.java
编译好以后,在D:\myjava\myimported会自动生成MyImportedClass.class文件。这样,在你的D:\myjava文件夹中的*.java文件可以import上述文件了:import myimported.MyImportedClass;public class MyClass
{
public static void main(String[] args)
{
MyImportedClass.message();
}
}运行当前代码,结果为:
hello
其实就是我在第一个帖子中所说的swing.JOptionPane.showInputDialog()方法,用这个方法,你不必再创建输入类,可以利用java本身提供的swing对话框得到输入。具体如下:import javax.swing.*;
public class Input
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog(null, "Enter a number"); int intNumber = Integer.parseInt(input);
// 或者可以转换成double
double doubleNumber = Double.parseDouble(input); System.out.println("Integer: "+intNumber);
System.out.println("Double: "+doubleNumber); System.exit(0);
}
}
1)token.TT_NUMBER 2)token.nval
token.nval: if the current token is a number, this field contains the value of
that number.