import java.io.*;
public class IsJNumber1{
public static void main(String args[]){
int a=0;
try{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
a=Integer.parseInt(s);
System.out.println(a);
}catch(IOException e){}
IsJNumber1 n=new IsJNumber1();
n.Number();
         switch(n.Number()){
         case 1:System.out.println("这是一个一位数!");break;
         case 2:System.out.println("这是一个两位数!");break;
         case 3:System.out.println("这是一个三位数!");break;
         case 4:System.out.println("这是一个四位数!");break;
         case 5:System.out.println("这是一个五位数!");break;
         
         case 6:System.out.println("输入错误!");break;
          }
}
static int Number(){
int a=0;
if(a>=0&&a<=9){
return a;
}
else if(a>=10&&a<=99){
return a;
}
else if(a>=100&&a<=999){
return a;
}
else if(a>=1000&&a<=9999){
return a;
}
else if(a>=10000&&a<=99999){
return a;
}
else{
    return a;
}
}
}
各位老师:为什么这段代码编译正确,可是就是不判断输入数值是几位数?

解决方案 »

  1.   

    你的a都是等于0,当然不输出了static int Number(){ 
    if(a>=0&&a <=9){ 
    return 1; 

    else if(a>=10&&a <=99){ 
    return 2; 

    else if(a>=100&&a <=999){ 
    return 3; 

    else if(a>=1000&&a <=9999){ 
    return 4; 

    else if(a>=10000&&a <=99999){ 
    return 5; 

    else{ 
        return 6; 
    }这样试一下
      

  2.   

    (1)
    static int a=0; 
    public static void main(String args[]){
    (2)
    public static void main(String args[]){ 
    int a=0;去掉
    (3)
    static int Number(){ 
    int a=0; //这句话去掉
    if(a>=0&&a <=9){ 
    return a; 

    (4)
    static int Number(){ 
    if(a>=0&&a <=9){ 
    return 1; 

    else if(a>=10&&a <=99){ 
    return 2; 

    else if(a>=100&&a <=999){ 
    return 3; 

    else if(a>=1000&&a <=9999){ 
    return 4; 

    else if(a>=10000&&a <=99999){ 
    return 5; 

    else{ 
        return a; 

      

  3.   

    import java.io.*;public class IsJNumber1 {
    static int a ;
    public static void main(String args[]) { try {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    a = Integer.parseInt("123");
    System.out.println(a);
    } catch (IOException e) {
    }
    switch (IsJNumber1.Number()) {
    case 1:
    System.out.println("这是一个一位数!");
    break;
    case 2:
    System.out.println("这是一个两位数!");
    break;
    case 3:
    System.out.println("这是一个三位数!");
    break;
    case 4:
    System.out.println("这是一个四位数!");
    break;
    case 5:
    System.out.println("这是一个五位数!");
    break; case 6:
    System.out.println("输入错误!");
    break;
    }
    } static int Number() {
    if (a >= 0 && a <= 9) {
    return 1;
    } else if (a >= 10 && a <= 99) {
    return 2;
    } else if (a >= 100 && a <= 999) {
    return 3;
    } else if (a >= 1000 && a <= 9999) {
    return 4;
    } else if (a >= 10000 && a <= 99999) {
    return 5;
    } else {
    return 6;
    }
    }
    }
      

  4.   

    上面的这一句修改一下 a = Integer.parseInt("123");
    a = Integer.parseInt(s);还有最好 不要转换为int 直接用string判断长度就搞定
      

  5.   

    如果你要转换最好加上
    String s = br.readLine();
    a = Integer.parseInt(s.trim());
      

  6.   

    import java.io.BufferedReader;
    import java.io.Closeable;
    import java.io.IOException;
    import java.io.InputStreamReader;public class Test5 {
        
        public static void main(String[] args) {
            BufferedReader br = null;
            try {
                br = getSystemReader();
                int num = inputInt("请输入一个整数: ", br);
                printResult(num);
            } finally {
                closeIO(br);
            }
        }
        
        /**
         * 打印结果
         * @param num
         *
         * @since 2009-9-11 上午11:56:04
         */
        private static void printResult(int num) {
            int len = getNumberLength(num);
            System.out.println("你所输入的数字为: " + num + ", 共有 " + len + " 位");
        }
        
        /**
         * 获得系统的输入流
         * @return
         *
         * @since 2009-9-11 上午11:56:13
         */
        private static BufferedReader getSystemReader() {
            return new BufferedReader(new InputStreamReader(System.in));
        }
        
        /**
         * 获得数字长度(不含符号)
         * @param num
         * @return
         * 
         * @since 2009-9-11 上午11:56:25
         */
        private static int getNumberLength(int num) {
            if(num < 0) {
                num = -num;
            }
            int len = 1;
            while(num > 9) {
                num /= 10;
                len++;
            }
            return len;
        }
        
        /**
         * 从系统输入流中输入一个数字
         * @param prompt    提示信息
         * @param reader    系统输入字符流
         * @return
         *
         * @since 2009-9-11 上午11:56:42
         */
        private static int inputInt(String prompt, BufferedReader reader) {        
            int result = 0;
            while(true) {
                try {
                    System.out.print(prompt);
                    result = Integer.parseInt(reader.readLine());
                    break;
                } catch(Exception e) {
                    System.out.println("  ## 输入错误,请重新输入 ##");
                }
            }
            return result;
        }
        
        /**
         * 关闭 IO 流
         * @param io
         *
         * @since 2009-9-11 上午11:57:14
         */
        private static void closeIO(Closeable io) {
            if(io != null) {
                try {
                    io.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }