需求:键盘录入一个九位数以下的数字将该数字各数字变为罗马数字。
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; } }
标签:return,Scanner,StringBuilder,练习,System,java,九位数 From: https://www.cnblogs.com/ayue6/p/17828912.html