62,615
社区成员
发帖
与我相关
我的任务
分享
package day04;
/*
* 用户输入一个正整数,输出相应英文;例如输入123,输出one two three
*/
import java.util.Scanner;
public class WhileDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = x;
int mode = 1;
while(y >=10){
y /=10;
mode*=10;
}
// System.out.println(mode);
while(mode>0){
switch(x/mode){
case 0 : System.out.print("zero");break;
case 1 : System.out.print("one");break;
case 2 : System.out.print("two");break;
case 3 : System.out.print("three");break;
case 4 : System.out.print("four");break;
case 5 : System.out.print("five");break;
case 6 : System.out.print("six");break;
case 7 : System.out.print("seven");break;
case 8 : System.out.print("eight");break;
case 9 : System.out.print("nine");break;
}
x %=mode;
mode/=10;
}
}
}
public class TestMain {
public static void main(String args[]){
String str = "-152";
for(int i=0;i<str.length();i++){
switch(str.charAt(i)){
case '-':System.out.print("minus");break;
case '0': System.out.print("zero");break;
case '1' : System.out.print("one");break;
case '2': System.out.print("two");break;
case '3' : System.out.print("three");break;
case '4' : System.out.print("four");break;
case '5' : System.out.print("five");break;
case '6' : System.out.print("six");break;
case '7' : System.out.print("seven");break;
case '8' : System.out.print("eight");break;
case '9' : System.out.print("nine");break;
}
System.out.print(" ");
}
}
/*
* 用户输入一个正整数,输出相应英文;例如输入123,输出one two three
*/
import java.util.Scanner;
public class WhileDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
if (x < 0) {
System.out.print("minus ");
x = Math.abs(x);
}
int y = x;
int mode = 1;
while(y >=10){
y /=10;
mode*=10;
}
// System.out.println(mode);
while(mode>0){
switch(x/mode){
case 0 : System.out.print("zero");break;
case 1 : System.out.print("one");break;
case 2 : System.out.print("two");break;
case 3 : System.out.print("three");break;
case 4 : System.out.print("four");break;
case 5 : System.out.print("five");break;
case 6 : System.out.print("six");break;
case 7 : System.out.print("seven");break;
case 8 : System.out.print("eight");break;
case 9 : System.out.print("nine");break;
}
x %=mode;
mode/=10;
System.out.print(" ");
}
}
}
输入/输出
-1561
minus one five six one
class Demo51
{
static final String[] number = {"minus", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
public static void main(String[] args) {
String str = "-122897436603273";
for (char c : str.toCharArray()) {
int _c = (_c = c - 47) == -2 ? 0 : _c;
System.out.print(number[_c] + ' ');
}
System.out.println();
}
}
再给一个完整的对数字合法性的检查
class Demo51
{
static final String[] number = {"minus", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
public static void main(String[] args) {
// 测试数据
// String str = "-122897436603273";
// String str = "-1228974-";
// String str = "122897436603273";
// String str = "+122";
// String str = "1+22";
String str = "123443a45c33";
try {
char[] values = str.toCharArray();
for (int i = 0; i < values.length; i++) {
int _c = (_c = values[i] - 47) == -2 ? 0 : _c;
if (_c == -4 && i == 0) continue; // 首位是+, 如+123
if (_c == 0 && i > 0) { // 负数但是-号不在第一位, 或者多个-号
throw new RuntimeException("不是一个合法的负数");
}
System.out.print(number[_c] + ' ');
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(str + "不是一个合法的数字");
} catch (RuntimeException e) {
e.printStackTrace();
}
System.out.println();
}
}