import java.io.InputStream;
public class JO {
public static void main(String args[]) {
InputStream ins =System.in;
byte b[] = new byte[100];
System.out.print("输入一行数字 : ");
try{
ins.read(b);
ins.close();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("输入的数字是 : "+new String(b));
int j = 0;
int o = 0;
for(int i=0; i<b.length; i++) {
int x = (int)b[i];
if(x>=0&&x<=9) {
if(b[i]%2==0) {
o++;
}else{
j++;
}
}else{
System.out.println("error");
break;
}
}
System.out.println("这些数里面的奇数个数为: "+j);
System.out.println("这些数里面的偶数个数为: "+o);
}
}这个程序就是输入一行数字,统计奇数偶数。调了一小时也没调对,要做到如果输入的不是数字就报error,而且输入几个数字就统计几个(不是一开始分配数组分配100就统计100)。谢谢各位高手了。
public class JO {
public static void main(String args[]) {
InputStream ins =System.in;
byte b[] = new byte[100];
System.out.print("输入一行数字 : ");
try{
ins.read(b);
ins.close();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("输入的数字是 : "+new String(b));
int j = 0;
int o = 0;
for(int i=0; i<b.length; i++) {
int x = (int)b[i];
if(x>=0&&x<=9) {
if(b[i]%2==0) {
o++;
}else{
j++;
}
}else{
System.out.println("error");
break;
}
}
System.out.println("这些数里面的奇数个数为: "+j);
System.out.println("这些数里面的偶数个数为: "+o);
}
}这个程序就是输入一行数字,统计奇数偶数。调了一小时也没调对,要做到如果输入的不是数字就报error,而且输入几个数字就统计几个(不是一开始分配数组分配100就统计100)。谢谢各位高手了。
import java.io.BufferedReader;
import java.io.InputStreamReader;import javax.swing.JOptionPane;public class JO{
public static void main(String args[]) throws Exception {
String str = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入一行数字 用,分隔:");
str = reader.readLine();
System.out.println(str);
while(str == null || "".equals(str.trim())){
JOptionPane.showMessageDialog(null,"输入不能为空,请重新输入");
str = reader.readLine();
}
System.out.println("输入的数字是 : " + str);
String[] temp = str.split(",");
int[] arr = new int[temp.length];
int j = 0;
int o = 0;
for(int i = 0 ;i < temp.length ; i++){
try{
arr[i] = Integer.parseInt(temp[i]);
if((arr[i]&1) != 0){
j ++;
}else {
o ++;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null,"输入数据有误,格式转换失败,程序将退出");
System.exit(0);
}
}
System.out.println("这些数里面的奇数个数为: " + j);
System.out.println("这些数里面的偶数个数为: " + o);
}
}
// 在你的基础上改改,觉得很别扭 还是贴出来,仅仅只能看看
// 弄懂一些道理,好的程序可不能这样写
class JO {
public static void test() {
InputStream ins = System.in;
byte b[] = new byte[100];
System.out.print("输入一行数字 : ");
int len = 0;
try {
len = ins.read(b);
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
// String strNum = b.toString();
String strNum = new String(b,0,len);
System.out.println("输入的数字是 : " + strNum);
System.out.println("输入的数字长度为 : " + strNum.length());
int j = 0;
int o = 0;
for (int i = 0; i < strNum.length()-2; i++) {//-2 去掉 \r\n
int x = strNum.charAt(i) - '0';
if (x >= 0 && x <= 9) {
if (b[i] % 2 == 0) {
o++;
} else {
j++;
}
} else {
System.out.println("error");
break;
}
}
System.out.println("这些数里面的奇数个数为: " + j);
System.out.println("这些数里面的偶数个数为: " + o);
}
}
import java.io.InputStream;public class TestSystemin {
public static void main(String args[]) {
InputStream ins = System.in;
byte b[] = new byte[100];
System.out.print("输入一行数字 : ");
int length=0;
try {
length = ins.read(b);
System.out.println("length:"+length);
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("输入的数字是 : " + new String(b));
int j = 0;
int o = 0;
for (int i = 0; i < length-2; i++) {
char x = (char) b[i];
x = (char)(x-48);
if (x >= 0 && x <= 9 ) {
if (x % 2 == 0) {
o++;
} else {
j++;
}
} else {
System.out.println("error");
return;
}
}
System.out.println("这些数里面的奇数个数为: " + j);
System.out.println("这些数里面的偶数个数为: " + o);
}
}