java之字符串练习2

1,

java之字符串练习2_第1张图片

import java.util.Scanner;
public class stringbuilderdemo2 {
    public static void main(String[] args) {
        //1,输入字符串
        System.out.println("please input a string");
        Scanner sc = new Scanner(System.in);
        StringBuilder s = new StringBuilder("");
        //2,数据校验
        while (true) {
            String str  = sc.next();
            if (str.length() > 9) {
                boolean flag = checkString(str);
                if(!flag){
                    System.out.println("wrong");
                }else{
                    //3,转罗马数字
                    for (int i = 0; i < str.length(); i++) {
                        char c = str.charAt(i);
                        s.append(transform(c)) ;
                    }
                    System.out.println(s);
                    break;

                }

            } else {
                System.out.println("wrong,please input again");
            }
        }


    }
    public static String transform(char num){
        //查表法
        String[] arr ={"","Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ"};
        int n = num -48;
        return arr[n];
    }
    public static boolean checkString(String str){
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            boolean flag= checkNum(c);
            if(!flag){
                return  false;
            }
        }
        return  true;
    }


    public static boolean checkNum(char s){
        if(s>='0' && s<='9')
        {
            return true;
        }
        else
            return false;
    }
}

你可能感兴趣的:(java语法,java,开发语言)