写个一个把阿拉伯数字转换成汉字的小程序。(如输入: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流没有关闭啊为什么程序都不等待键盘输入就直接不停的显示:“请输入阿拉伯数字:”、“您输入为空!”的死循环啊!!
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流没有关闭啊为什么程序都不等待键盘输入就直接不停的显示:“请输入阿拉伯数字:”、“您输入为空!”的死循环啊!!
解决方案 »
- createSQLQuery("{call procName()}")不适用于sqlserver?
- 哪位兄弟对网络编程比较熟悉,进来帮个忙!
- java环境下,如何动态的装在一个指定 jar 文件中的一个类
- 这几句话怎样解释,我不明白,帮忙!!!!!跟贴有分。
- 谁能介绍一下JAXML?
- 请问如何得到Java程序句柄?
- 5个小问题,每一个10分相赠!
- 在java中可以调用excel函数库吗?
- 为什么JBuilder 4.0打包后不能脱离JBuilder???
- 怎樣控制JTable中各行的寬度﹐使各行寬度不相同﹗﹗謝謝﹗﹗
- 求解十六进制转换为字符串
- 点击tomcat 5.5\bin下startup.bat一闪而过!求助为啥???
比如我输入:123---
则输出:一百二十三 //第一次循环正常。
您输入为空 //第二次循环的时候我根本什么都没输入,它就提示输入为空,然后结束进程。
(程序结束)
第二次不是data不是应该等待键盘输入吗?为什么还没输入呢,就显示输入为空了呢?!
boolean isTrue = true;
while (isTrue) {
Scanner input = new Scanner(System.in);
String str;
System.out.println("请输入需要翻译的阿拉伯数字:");
str = input.next();
System.out.println(format(str));
}
}
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捕获,输出您输入的字符串为空!:,然后继续循环,就一直报错了,呵呵