首页 > 其他分享 >21.text--转换罗马数字

21.text--转换罗马数字

时间:2023-04-12 10:55:20浏览次数:27  
标签:case 21 -- text continue str SB append String

转换罗马数字

键盘录入一个字符串
要求1:长度为小于等于9
要求2:只能是数字
将内容变成罗马数字
|-1 , ||-2 , |||-3 , |V-4 , V-5 , V|-6 , V||-7 , V|||-8 , |X-9
注意点:罗马数字里面是没有0的,如果键盘录入的数字包含0,可以变成“”(长度为0的字符串)

public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str;
        while (true) {
            System.out.println("请输入一个长度 ≤ 9的字符串:");
            str = s.next();

            //要求1、要求2,调用方法
            boolean flag = checkStr(str);
            if(flag){
                break;
            }else{
                System.out.println("输入有误,请重新输入!");
            }
        }

//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < str.length(); i++) {
//            char c = str.charAt(i);
//            int number = c - 48;
//            String novelStr = changeStr(number);
//            sb.append(novelStr).append(" ");
//        }

        String sb = changeLuoMa1(str);

        System.out.println(sb);

        s.close();
    }

    public static boolean checkStr(String str){
        //要求1:长度为小于等于9
        if(str.length() >= 9){
            return false;
        }
        //要求2:只能是数字
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if(c <= '0' || c >= '9'){
                return false;
            }
        }
        return true;
    }
//将内容变成罗马数字,|-1 , ||-2 , |||-3 , |V-4 , V-5 , V|-6 , V||-7 , V|||-8 , |X-9
    public static String changeLuoMa1(String str){
        StringBuilder SB = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            switch (str.charAt(i)){
                case '0':
                    SB.append(" ");
                    continue;
                case '1':
                    SB.append("| ");
                    continue;
                case '2':
                    SB.append("|| ");
                    continue;
                case '3':
                    SB.append("||| ");
                    continue;
                case '4':
                    SB.append("|V ");
                    continue;
                case '5':
                    SB.append("V ");
                    continue;
                case '6':
                    SB.append("V| ");
                    continue;
                case '7':
                    SB.append("V|| ");
                    continue;
                case '8':
                    SB.append("V||| ");
                    continue;
                case '9':
                    SB.append("|X ");
                    continue;
            }
        }
        return SB.toString();
    }

标签:case,21,--,text,continue,str,SB,append,String
From: https://www.cnblogs.com/Zz1001/p/17309048.html

相关文章

  • 20.text--对称字符串
    对称字符串键盘接收一个字符串,程序判断出该字符串是否是对称字符串,并在控制台打印是或不是例如:对称字符串123321、111非对称字符串123123publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);System.out.println("请输入......
  • 108. 将有序数组转换为二叉搜索树
    给你一个整数数组nums,其中元素已经按升序排列,请你将其转换为一棵高度平衡二叉搜索树。高度平衡二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过1」的二叉树。classSolution{public:TreeNode*sortedArrayToBST(vector<int>&nums){......
  • template
    template(......
  • vue项目通过外部配置文件读取接口地址- 在webpack-index.html模板中使用环境变量
    概述:在index.html模板中判断当前环境,处于开发环境下时读取process环境变量、处于生产环境下时读取根目录配置文件(./config.js),两种环境下将配置统一挂载到window全局变量上(SET_CONFIG)config.jswindow.SITE_CONFIG={appTitle:'系统测试',version:'1.0.0',apiURL:''......
  • JavaScript Window.Location - 获取当前页面地址(URL)并重定向到新页面。
    JavaScriptWindow.Location-获取当前页面地址(URL)并重定向到新页面。1、常用:window.location.href//返回当前页面的href(URL)window.location.host//或window.location.hostname//返回web主机的域名或IP地址,window.location.pathname//返回当前页面的路径或......
  • 玖章算术CEO叶正盛在数据技术嘉年华分享NineData AIGC的应用实践
    4月8日下午,为期两天的第十二届数据技术嘉年华(DTC2023)在北京新云南皇冠假日酒店圆满落下帷幕。大会得到了工业和信息化部电子五所的支持和指导,围绕“开源·融合·数字化——引领数据技术发展,释放数据要素价值”这一主题,通过一场主论坛和十二场专题论坛,汇聚“产学研”各界数据技术......
  • UVa 10673 Play with Floor and Ceil (数论)
    10673-PlaywithFloorandCeilTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1614TheoremForanytwointegers x and k thereexiststwomoreintegers p ......
  • UVa 10004 Bicoloring (DFS&二分图)
    10004-BicoloringTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=945In1976the``FourColorMapTheorem"wasprovenwiththeassistanceofacomput......
  • UVa 143 Orchard Trees (数学&计算几何&枚举)
    143-OrchardTreesTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=79AnOrchardisthasplantedanorchardinarectanglewithtreesuniformlyspacedinbothdirections.T......
  • LoRa水压传感器解决方案
    lora压力传感器 采用LoRa扩频技术的一款管道压力传感器。能够实时检测水、油、气等多种介质管道压力。通过LoRaWAN低功耗传感器网络传输到远程平台。传感器内置19000mAH大容量锂亚电池,可连续工作数年。支持智能的物联网平台,实现远程数据监控和管理。无线压力监测报警器是一款锂电......