java的一个小程序,判断输入数字的位数,要用到异常处理

java的一个小程序,判断输入数字的位数,要用到异常处理,第1张

你的getnum方法 定义的时候需要返回int ,但你的方法体里面没有return,报错了

其实这个地方不用返回

            public void getnum(int i) throws AAException {

                if (i < 0 || i > 9999) {

                    throw new AAException()

                }

                if (i >= 0 && i <= 9)

                    System.out.print("一位数")

                else if (i >= 10 && i <= 99)

                    System.out.print("两位数")

                else if (i >= 100 && i <= 999)

                    System.out.print("三位数")

                else if (i >= 1000 && i <= 9999)

                    System.out.print("四位数")

            }

在调用的时候,这样写

        try {

            String s = stdin.nextLine()

            double ii = Double.parseDouble(s)

            int i = (int) ii

            a.getnum(i)

        } catch (AAException e) {

            System.out.println(e.toString())

        }

合并版本的代码:

#include <stdlib.h>

#include <stdio.h>

#include <math.h>

static char cha = 'o'

static char chb = 'x'

static char chc = 'U'

static char chd = 'D'

static char chx = 'o'

static char chy = 'x'

int isSSZ(int x, int y) // y在x的顺时针方向,距离限制1到4

{

switch(x)

{

case '0': case '1': case '2':

case '3': case '4': case '5': return (y>=x&&y<=x+4)?1:0

case '6': return (y=='7'||y=='8'||y=='9'||y=='0')?1:0

case '7': return (y=='8'||y=='9'||y=='0'||y=='1')?1:0

case '8': return (y=='9'||y=='0'||y=='1'||y=='2')?1:0

case '9': return (y=='0'||y=='1'||y=='2'||y=='3')?1:0

default : return 0

}

}

void main(int argc, char* argv[])

{

if(argc<3)

{

printf("Error.\nno input file or output file.\n")

system("pause")

exit(-2)

}

FILE *fpi,*fpo

char num[50]

char bin[][5] = { "000", "000", "000", "000", "000" }

char loop[][5] = { "000", "000", "000", "000", "000" }

char tmp[4] = { '\0', '\0', '\0', '\0' }

fpi=fopen(argv[1],"rt")

fpo=fopen(argv[2],"wt")

int count = 0, ixx = 0

if(!fpi||!fpo)

{

printf("Error open input file or output file.")

system("pause")

exit(-1)

}

while(!feof(fpi))

{

if( fgets( num, 50, fpi ) != NULL)

{

count++

if(count%30==1) fprintf(fpo,"num,bin01,bin02,bin03,bin04,bin05,loop1,loop2,loop3,loop4,loop5\n")

num[3]=loop[0][3]=loop[1][3]=loop[2][3]=loop[3][3]=loop[4][3]='\0'

num[3]=bin[0][3]=bin[1][3]=bin[2][3]=bin[3][3]=bin[4][3]='\0'

bin[0][0]=(num[0]=='0')?chx:((num[0]=='7')?chy:((num[0]=='1'||num[0]=='3'||num[0]=='5'||num[0]=='9')?'1':'2'))

bin[0][1]=(num[1]=='0')?chx:((num[1]=='7')?chy:((num[1]=='1'||num[1]=='3'||num[1]=='5'||num[1]=='9')?'1':'2'))

bin[0][2]=(num[2]=='0')?chx:((num[2]=='7')?chy:((num[2]=='1'||num[2]=='3'||num[2]=='5'||num[2]=='9')?'1':'2'))

bin[1][0]=(num[0]=='1')?chx:((num[0]=='8')?chy:((num[0]=='0'||num[0]=='3'||num[0]=='5'||num[0]=='9')?'1':'2'))

bin[1][1]=(num[1]=='1')?chx:((num[1]=='8')?chy:((num[1]=='0'||num[1]=='3'||num[1]=='5'||num[1]=='9')?'1':'2'))

bin[1][2]=(num[2]=='1')?chx:((num[2]=='8')?chy:((num[2]=='0'||num[2]=='3'||num[2]=='5'||num[2]=='9')?'1':'2'))

bin[2][0]=(num[0]=='2')?chx:((num[0]=='9')?chy:((num[0]=='0'||num[0]=='3'||num[0]=='5'||num[0]=='8')?'1':'2'))

bin[2][1]=(num[1]=='2')?chx:((num[1]=='9')?chy:((num[1]=='0'||num[1]=='3'||num[1]=='5'||num[1]=='8')?'1':'2'))

bin[2][2]=(num[2]=='2')?chx:((num[2]=='9')?chy:((num[2]=='0'||num[2]=='3'||num[2]=='5'||num[2]=='8')?'1':'2'))

bin[3][0]=(num[0]=='3')?chx:((num[0]=='4')?chy:((num[0]=='1'||num[0]=='2'||num[0]=='5'||num[0]=='8')?'1':'2'))

bin[3][1]=(num[1]=='3')?chx:((num[1]=='4')?chy:((num[1]=='1'||num[1]=='2'||num[1]=='5'||num[1]=='8')?'1':'2'))

bin[3][2]=(num[2]=='3')?chx:((num[2]=='4')?chy:((num[2]=='1'||num[2]=='2'||num[2]=='5'||num[2]=='8')?'1':'2'))

bin[4][0]=(num[0]=='5')?chx:((num[0]=='6')?chy:((num[0]=='1'||num[0]=='2'||num[0]=='3'||num[0]=='8')?'1':'2'))

bin[4][1]=(num[1]=='5')?chx:((num[1]=='6')?chy:((num[1]=='1'||num[1]=='2'||num[1]=='3'||num[1]=='8')?'1':'2'))

bin[4][2]=(num[2]=='5')?chx:((num[2]=='6')?chy:((num[2]=='1'||num[2]=='2'||num[2]=='3'||num[2]=='8')?'1':'2'))

fprintf( fpo, "%s", num )

fprintf( fpo, "," )

fprintf( fpo, "%s,%s,%s,%s,%s", bin[0], bin[1], bin[2], bin[3], bin[4] )

if(count==1)

{

fprintf( fpo, "\n" )

tmp[0]=num[0]tmp[1]=num[1]tmp[2]=num[2]

continue

}

ixx=(num[0]-tmp[0])%10ixx=(ixx>=0)?ixx:(10+ixx)

loop[0][0]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[0],tmp[0])?chc:chd))

loop[1][0]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[0],tmp[0])?chc:chd))

loop[2][0]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[0],tmp[0])?chc:chd))

loop[3][0]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[0],tmp[0])?chc:chd))

loop[4][0]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[0],tmp[0])?chc:chd))

ixx=(num[1]-tmp[1])%10ixx=(ixx>=0)?ixx:(10+ixx)

loop[0][1]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[1],tmp[1])?chc:chd))

loop[1][1]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[1],tmp[1])?chc:chd))

loop[2][1]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[1],tmp[1])?chc:chd))

loop[3][1]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[1],tmp[1])?chc:chd))

loop[4][1]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[1],tmp[1])?chc:chd))

ixx=(num[2]-tmp[2])%10ixx=(ixx>=0)?ixx:(10+ixx)

loop[0][2]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[2],tmp[2])?chc:chd))

loop[1][2]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[2],tmp[2])?chc:chd))

loop[2][2]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[2],tmp[2])?chc:chd))

loop[3][2]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[2],tmp[2])?chc:chd))

loop[4][2]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[2],tmp[2])?chc:chd))

fprintf( fpo, "," )

fprintf( fpo, "%s,%s,%s,%s,%s", loop[0], loop[1], loop[2], loop[3], loop[4] )

fprintf( fpo, "\n" )

tmp[0]=num[0]tmp[1]=num[1]tmp[2]=num[2]

} else

printf( "done. %d line(s) processed.\n", count)

}

fclose(fpi)

fclose(fpo)

system("pause")

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8101151.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存