俺才学的,本来也想说这么写 class A { public void fan(String s) { String s1[]=new String[2]; for(int i=0;i<s.length();i++) { s1[i]=fanhui(s.charAt(i)); } System.out.println (s1[0]+"-"+s1[1]);
} public String fanhui(char ch) { switch(ch){ case '1':return "one"; case '2':return "two"; case '3':return "three"; default:return "0"; } } } 不过貌似还需要写十位。用数组把1,2,3……10,20,30……放里面?然后判断下长度?拼起来?
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入数字串:"); int num = input.nextInt(); System.out.println(convert(num)); }
System.out.print("sixty-eight");
}
int t = ch -'0';
switch(t){
case 1 : return "one";
break;
.
.
.
.
}
}
fun2(String str){
StringBuffer result = new StringBuffer ("");
for(int i =0; i < str.length - 1; i ++){
result.append(fun(str.charAt(i)));
}
result.append(fun(str.charAt(str.length - 1)));}
大概这个意思貌似 没编译器
System.out.println(nums[i]);
}
class A
{
public void fan(String s)
{
String s1[]=new String[2];
for(int i=0;i<s.length();i++)
{
s1[i]=fanhui(s.charAt(i));
}
System.out.println (s1[0]+"-"+s1[1]);
}
public String fanhui(char ch)
{
switch(ch){
case '1':return "one";
case '2':return "two";
case '3':return "three";
default:return "0";
}
}
}
不过貌似还需要写十位。用数组把1,2,3……10,20,30……放里面?然后判断下长度?拼起来?
import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入数字串:");
int num = input.nextInt();
System.out.println(convert(num));
}
public static String convert(int num) {
String[] enumber = {"zero","one","two","three","four","five","six","seven","eight","nine"};
StringBuffer sb = new StringBuffer();
String temp = num + "";
for(int i=0; i<temp.length(); i++) {
sb.append(enumber[temp.charAt(i) - 48] + "-");
}
temp = sb.toString();
return temp.substring(0,temp.length() - 1);
}
}我写了一个,楼主可以看看!
少 ty
我觉着好事,枚举实用
public static void trsfm(int num) {
String[][] str = {
{"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"},
{"null", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eithty", "ninety"},
};
int[] arr = new int[10];
int k = 0;
while(num != 0) {
arr[k] = num % 10;
num /= 10;
k++;
}
System.out.println(k);
for(int i = 0; i < k; i++)
System.out.println(arr[i]);
for( k -= 1; k >= 0; k--)
if(k != 0)
System.out.print(str[k][arr[k]] + "-");
else
System.out.print(str[k][arr[k]]);
}
就两位的!三位和三位以上的话也比较有规律的
hundred 与 thousand 分别保存一下……
System.out.println(s);
}
public static String printResult(int i){
String result = "";
String numbers[] = {"zero","one","two","three","four","five","six","seven","eight","nine"
,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
String tens_digit[] = {"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
boolean bool = false ;
ArrayList<Integer> list = new ArrayList<Integer>();
if(i<0){
i = Math.abs(i);
bool = true;
}
if(i < 20){
result = numbers[i];
}else if(i == 20){
result = tens_digit[2];
}else{
int a = i;
while(a > 0){
int j = a%10;
a /= 10;
list.add(j);
}
if(list.size() == 2){
result = tens_digit[list.get(1)] + "-" + numbers[list.get(0)];
}else if(list.size() == 3){
result = printResult(i/100) + " hundred and " + printResult(i%100);
}else if(list.size() < 7){
result = printResult(i/1000) + " thousand " + printResult(i%1000);
}else if(list.size() < 10){
result = printResult(i/1000000) + " million " + printResult(i%1000000);
}else{
result = printResult(i/1000000000) + " billion " + printResult(i%1000000000);
}
}
if(bool){
result = "negative " + result;
}
return result;
}
}