java(StringBuilder)练习

需求:键盘录入一个九位数以下的数字将该数字各数字变为罗马数字。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
public class Boke {
    public static void main(String[] args){
        Scanner Sc= new Scanner(System.in);
        String x;
        while(true){
            System.out.println("请输一个小于九位数的数字:");
             x = Sc.next();
            //判断
            boolean agls = Ayue(x);
            if(agls){
                break;
            }else{
                System.out.println("当前的字符串不符合规则,请重新输入");
                continue;
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i =0;i<x.length();i++){
            char c = x.charAt(i);
//            System.out.println(c);
            int y =c-48;
            String s= Luoma(y);
            sb.append(s+" ");
        }
        System.out.println(sb);
    }
 
    public static String Luoma(int y){
        String arr[] = {" ","I","II","III","IV","V","VI","VII","VIII","IX"};
        return arr[y];
    }
    public static boolean Ayue(String x){
        if(x.length()>9){
            return false;
        }
        for (int i = 0; i < x.length(); i++) {
            char c = x.charAt(i);
            if(c<'0'||c>'9'){
                return false;
            }
        }
        return true;
    }
}

  

 

posted @   小菜阿跃  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示