写个一个把阿拉伯数字转换成汉字的小程序。(如输入:123456789---则输出:壹億貳仟叁佰肆拾伍萬陸仟柒佰捌拾玖 )先写好一次执行的:运行没有问题。(代码如下)后来加了个while想让程序多次执行,可是出现了死循环一次执行的代码:(可运行)package one;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;public class SimpleMoneyNumber {
public static final String ZERO = "零";
public static final String ONE = "壹";
public static final String TWO = "貳";
public static final String THREE = "叁";
public static final String FOUR = "肆";
public static final String FIVE = "伍";
public static final String SIX = "陸";
public static final String SEVEN = "柒";
public static final String EIGHT = "捌";
public static final String NINE = "玖";
public static final String TEN = "拾";
public static final String HUNDRED = "佰";
public static final String THOUSAND = "仟";
public static final String WANG = "萬";
public static final String MILLION = "億";
public static String str = null; public static String format(String str) {
SimpleMoneyNumber.str = str;
String[] dangwei = {THOUSAND,HUNDRED,TEN,MILLION,THOUSAND,HUNDRED,TEN,WANG,THOUSAND,HUNDRED,TEN," "};
//将输入的字符串转换为字符数组
char[] num = str.toCharArray();
//newNum字符数组用于存储阿拉伯数字对应的中文数字
String[] newNum = new String[num.length];
//newDangwei用于存储需要用到的对应的单位
String[] newDangwei;
//show字符串用于表示最后输出的信息
String show = "";
//填充newDangwei数组
newDangwei = Arrays.copyOfRange(dangwei,12-num.length,12);

//将阿拉伯字符对应的中文字符写到newNum数组的相应位置
for (int i=0; i<num.length; i++) {
switch (num[i]) {
case '0': newNum[i] = ZERO;break; 
case '1': newNum[i] = ONE;break;  
case '2': newNum[i] = TWO;break;  
case '3': newNum[i] = THREE;break;  
case '4': newNum[i] = FOUR;break;  
case '5': newNum[i] = FIVE;break;  
case '6': newNum[i] = SIX;break;  
case '7': newNum[i] = SEVEN;break;  
case '8': newNum[i] = EIGHT;break;  
case '9': newNum[i] = NINE;break; 
default:
System.out.println("含有非法字符,请重新输入:");break; 
}
//将数字数组和单位数组组合并加到字符串show的后面。
show += newNum[i] + newDangwei[i];
}

return show;
} public static void main(String[] args) {
BufferedReader data = null;
data = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("请输入需要翻译的阿拉伯数字:");
try{
str = data.readLine();
System.out.println(format(str));
try{
data.close();
}
catch (IOException e) {
System.out.println(e);
}
}
catch (IOException e) {
System.out.println("您输入的字符串为空!:");
}
finally {
if (data != null) {
try{
data.close();
}
catch (IOException e) {
System.out.println("输入流关闭异常!");
}
}
}
System.out.println();
}
}
二次执行的方法改了下main()方法,如下(死循环)public static void main(String[] args) {
boolean isTrue = true;
BufferedReader data = null;
data = new BufferedReader(new InputStreamReader(System.in));

while (isTrue) {
String str;
System.out.println("请输入需要翻译的阿拉伯数字:");
try{
str = data.readLine();
System.out.println(format(str));
try{
data.close();
}
catch (IOException e) {
System.out.println(e);
}
}
catch (IOException e) {
System.out.println("您输入的字符串为空!:");
}
finally {
if (data != null) {
try{
data.close();
}
catch (IOException e) {
System.out.println("输入流关闭异常!");
}
}
}
}
}
是不是IO流没有关闭啊为什么程序都不等待键盘输入就直接不停的显示:“请输入阿拉伯数字:”、“您输入为空!”的死循环啊!!

解决方案 »

  1.   

    在异常中将isTrue变量赋值false就OK了。
      

  2.   

    改了也没用啊,我把改后的过程说一下。
    比如我输入:123---
    则输出:一百二十三  //第一次循环正常。
                 您输入为空   //第二次循环的时候我根本什么都没输入,它就提示输入为空,然后结束进程。
    (程序结束)
    第二次不是data不是应该等待键盘输入吗?为什么还没输入呢,就显示输入为空了呢?!
      

  3.   

    应该是第一次循环后就关闭了输入流,第二次循环时data.readLine();出现异常执行了catch语句,while循环一直执行
      

  4.   

    public static void main(String[] args) {
    boolean isTrue = true;
    while (isTrue) {
    Scanner input = new Scanner(System.in);
    String str;
    System.out.println("请输入需要翻译的阿拉伯数字:");
    str = input.next();
    System.out.println(format(str));
    }
    }
      

  5.   

    恩,这样也行,如果只new一次,那么不能关闭输入流,只能在while执行完才能关闭;如果每次循环都关闭,则每次都要new一次输入流。谢谢啊。
      

  6.   

    这个写的好简洁,没用Scanner,回头好好学习。谢谢啊。
      

  7.   


    while (isTrue) {
                    String str;
                    System.out.println("请输入需要翻译的阿拉伯数字:");
                    try{
                        str = data.readLine();
                        System.out.println(format(str));
                        try{
                            data.close();
                        }
                        catch (IOException e) {
                            System.out.println(e);
                        }
                    }
                    catch (IOException e) {
                        System.out.println("您输入的字符串为空!:");
                    }
                    finally {
                        if (data != null) {
                            try{
                                data.close();
                            }
                            catch (IOException e) {
                                System.out.println("输入流关闭异常!");
                            }
                        }
                    }
                }
    输入流在第一次输入后被关了,第二次用data.readLine时就报异常了,被catch捕获,输出您输入的字符串为空!:,然后继续循环,就一直报错了,呵呵
      

  8.   

    Scanner有时真的很好用直接提供了hasNextInt() nextInt 等有用其简单的方法