java(StringBuilder)练习

需求:键盘录入一个九位数以下的数字将该数字各数字变为罗马数字。
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 @ 2023-11-13 13:24  小菜阿跃  阅读(18)  评论(0)    收藏  举报