黑马程序员技术交流社区
标题:
编程题,帮帮忙
[打印本页]
作者:
李洋-
时间:
2013-12-17 15:08
标题:
编程题,帮帮忙
从键盘接受一个数字,列出该数字的中文表示格式。
例如:键盘输入123,打印出一二三,键盘输入3103,打印出三一零三。
作者:
FFF
时间:
2013-12-17 16:03
太简单了。没啥挑战性。
你可以用System.in接收数据。再String转char再用查表法。好简单的呢~
作者:
末末
时间:
2013-12-17 16:28
public class Demo5
{
public static void main(String[] args) throws IOException
{
InputStream in=System.in;
BufferedReader br=new BufferedReader(new InputStreamReader(in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
String line=null;
char[] ch={'0','1','2','3','4','5','6','7','8','9'};
char[] ch2={'零','壹','贰','叁','肆','伍','陆','柒','捌'};
while((line=br.readLine())!=null)
{
if("over".equals(line))
{
break;
}
bw.write(show(line,ch,ch2));
bw.flush();
}
bw.close();
br.close();
}
public static char[] show(String line,char[] ch,char[] ch2)
{
char[] s=line.toCharArray();
for(int x=0;x<s.length;x++)
{
for(int y=0;y<ch.length;y++)
{
if(s[x]==ch[y])
s[x]=ch2[y];
}
}
return s;
}
}
复制代码
作者:
李洋-
时间:
2013-12-17 16:50
FFF 发表于 2013-12-17 16:03
太简单了。没啥挑战性。
你可以用System.in接收数据。再String转char再用查表法。好简单的呢~ ...
那你帮忙给个代码演示呗
作者:
belief丶Only
时间:
2013-12-17 17:30
我是在控制台输入的,你可以参考一下!!没有判断其他异常,只能输入数字,负数也可以
import java.util.Scanner;
public class TestOutput{
/*定义两个字符型数组,一个是数字,一个数汉字,
* 注意:数字要对应汉字的下角标,这样好通过数组的index赋值*/
char [] cha = {'0','1','2','3','4','5','6','7','8','9','-'};
char [] chin = {'零','一','二','三','四','五','六','七','八','九','负'};
public void transition(String str)//定义方法,接收传过来的数值字符串
{
/*//自定义一个空的字符型数组,用于装转换好的汉字*/
char[] chs = new char[str.length()];
for (int i = 0; i < str.length(); i++)//这一步是循环你输入的数字
{
for (int j = 0; j < cha.length; j++) {//这是循环是你定义好的字符数组cha[]
if(str.charAt(i)==cha[j])//判断你输入的书否等于其中的
{
chs[i] += chin[j];//如果等于,就将汉字的数组相对的索引赋值
} //这里就体现了数字要对应汉字的下角标
}
}
System.out.println(chs);
}
public void inistal() {
Scanner s = new Scanner(System.in);
System.out.println("请输入数字:");
String str = s.next();
transition(str);//调用上面的方法
}
public static void main(String[]args)
{
TestOutput o = new TestOutput();
o.inistal();
}
}
复制代码
作者:
闭眼就天黑
时间:
2013-12-17 22:57
看到答案懂了,{:soso_e128:}每天看看问题收集里面的答案
作者:
张鹏
时间:
2013-12-18 23:58
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
char[] chars = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
Scanner inputScanner = new Scanner(System.in);
System.out.println("请输入数字:");
String string = inputScanner.next();
if (!string.matches("\\d+"))
{
System.out.println("不合法的数字!");
} else
{
for (int i = 0, le = string.length(); i < le; i++)
{
System.out.print(chars[string.charAt(i) - 48]);
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2