下面的程序是从键盘获得一个字符
import java.io.*;
class QuestionTest
{
public static void main(String args[]) throws IOException
{
byte[] a={'1'};
boolean b=true;
System.in.read(a);
System.out.println((char)a[0]);
}
}以后问问题要记得给分数,这样别人才会回答你的问题,也是对别人的尊重呀:)
import java.io.*;
class QuestionTest
{
public static void main(String args[]) throws IOException
{
byte[] a={'1'};
boolean b=true;
System.in.read(a);
System.out.println((char)a[0]);
}
}以后问问题要记得给分数,这样别人才会回答你的问题,也是对别人的尊重呀:)
import java.io.*;
class QuestionTest
{
public static void main(String args[])
{
String str="";
BufferedReader br=new BufferedReader(new InputStreamReader (System.in)) ;
try{
str=br.readLine();
}
catch(Exception e){}
}
}
可以这样(借助楼上的进行修改)import java.io.*;
class QuestionTest
{
public static void main(String args[])
{
String str = "";
int value = 0;
BufferedReader br=new BufferedReader(new InputStreamReader (System.in)) ;
try {
while (true) {
//System.out.print("Input the integer value you want:");
str = br.readLine();
try {
value = Integer.parseInt(str);
break;
} catch (Exception ex) {
System.out.println("Input error, please input again.");
}
}
br.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
public static void main (String [] args){
string keystroke=0;
System.out.println("Enter a number, please:");
try{
keystroke = system.in.read();
}catch (IOException e){
System.out.println ("Error!");
}
System.out.println("The number is"+keystroke);
}
}
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.NumberFormatException;
public class StandardInputReader extends BufferedReader { /**
* Constructs a new input stream with the specified standard input stream
*/
public StandardInputReader() {
super(new InputStreamReader(System.in));
} /**
* Read a boolean value from standard input stream.
*
* @return a boolean value.
* @throws IOException
*/
public boolean readBoolean() throws IOException {
String strBoolean = super.readLine(); if (strBoolean.equalsIgnoreCase("true")) {
return true;
} else if (strBoolean.equalsIgnoreCase("false")) {
return false;
} else {
throw new NumberFormatException("For input string: \""
+ strBoolean +"\"");
}
} /**
* Read a byte value from standard input stream.
*
* @return a byte value.
* @throws IOException
*/
public byte readByte() throws IOException {
return Byte.parseByte(super.readLine());
} /**
* Read a short value from standard input stream.
*
* @return a short value.
* @throws IOException
*/
public short readShort() throws IOException {
return Short.parseShort(super.readLine());
} /**
* Read a int value from standard input stream.
*
* @return a int value.
* @throws IOException
*/
public int readInt() throws IOException {
return Integer.parseInt(super.readLine());
} /**
* Read a long value from standard input stream.
*
* @return a long value.
* @throws IOException
*/
public long readLong() throws IOException {
return Long.parseLong(super.readLine());
} /**
* Read a float value from standard input stream.
*
* @return a float value.
* @throws IOException
*/
public float readFloat() throws IOException {
return Float.parseFloat(super.readLine());
} /**
* Read a double value from standard input stream.
*
* @return a double value.
* @throws IOException
*/
public double readDouble() throws IOException {
return Double.parseDouble(super.readLine());
}
}
public class QuestionTest {
public static void main (String [] args){
int keystroke=0;
DataInputStream dis = new DataInputStream (System.in);
System.out.println("Enter a number, please:");
boolean succ = true; //
try{
keystroke = dis.readInt();
}catch (IOException e){
System.out.println ("Error!");
succ = false; //
}
if(succ)
System.out.println("The number is"+keystroke);
}
}