日期:2014-05-20  浏览次数:20817 次

我的输入这块 什么问题呀?请赐教!
import java.io.IOException;
import java.io.InputStreamReader;

public class EvenNumber {

public static void main(String[] args) {

InputStreamReader isr = new InputStreamReader(System.in);

try {
int number = isr.read();
System.out.print("Is " + number + " an even number?");
if (number / 2 == 0) {
System.out.println("true");
} else {
System.out.println("false");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


------解决方案--------------------
我改了改~你看看好使不~
import java.util.Scanner;

public class Test {

public static void main(String[] args) {
System.out.println("请输入一个数:");
Scanner isr = new Scanner(System.in);

int number = isr.nextInt();
System.out.print("Is " + number + " an even number?");
if (number % 2 == 0) {
System.out.println("true");
} else {
System.out.println("false");
}

}
}
------解决方案--------------------
呵呵,我也来改
import java.util.Scanner; 

public class TestEvenOdd { 
public static void main(String[] args) { 
int number=0;
System.out.println("输入数字后回车检验奇偶数,输-1退出:"); 
Scanner sc = new Scanner(System.in); 
while((number = sc.nextInt())!=-1){
if (number % 2 == 0) { 
System.out.println(number + " is a EvenNumber"); 
} else { 
System.out.println(number+ " is a OddNumber"); 

}


}
------解决方案--------------------

完整修改如下:当输入0的时候才能得到true,应该是%操作。

import java.io.IOException;
import java.io.InputStreamReader;

public class EvenNumber {

public static void main(String[] args) {

InputStreamReader isr = new InputStreamReader(System.in);

try {

int number = isr.read();
System.out.println(number);
char n = (char) number;
System.out.println(n);
String s = String.valueOf(n);
System.out.println(s);
number = Integer.valueOf(s);
System.out.println(number);
System.out.print("Is " + number + " an even number?");
System.out.println(number / 2);
if (number / 2 == 0) {
System.out.println("true");
} else {
System.out.println("false");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
------解决方案--------------------
通用控制台输入代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class EvenNumber {

public static void main(String[] args) {

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(isr);

try {
String s = reader.readLine();
System.out.println(s);
int number = Integer.valueOf(s);

System.out.println(number);
System.out.print("Is " + number + " an even number?");

if (number % 2 == 0) {
System.out.println("true");
} else {
System.out.println("false");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}