JAVA练习:罗马数字
题目:
1.实现思路:
2.实现截图:
3.代码:
import java.util.Scanner;
public class zhlmsz {
public static void main(String[] args) {
String oldabc;
while(true){
System.out.println("请输入一个字符串:");
Scanner sc = new Scanner(System.in);
String abc = sc.next();
if (pd(abc)==true) {
oldabc=abc;
break;
}else if(pd(abc)==false){
System.out.println("当前字符串不符合规则,请重新输入:");
continue;
}
}
String newabc="";
for (int i = 0; i < oldabc.length(); i++) {
newabc=newabc+lmsz(szzh(oldabc.charAt(i)));
newabc+=" ";
}
System.out.println(newabc);
}
public static boolean pd(String a){
if(a.length()>9){return false;}
for (int i = 0; i < a.length(); i++) {
if(a.charAt(i)<'0'||a.charAt(i)>'9'){
return false;
}
}
return true;
}
public static String lmsz(int a){
String[] aaa = {" ", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};
return aaa[a];
}
public static int szzh(char a){
int c=0;
c=a-48;
return c;
}
}
import java.util.Scanner;
public class zhlmsz {
public static void main(String[] args) {
String oldabc;
while(true){
System.out.println("请输入一个字符串:");
Scanner sc = new Scanner(System.in);
String abc = sc.next();
if (pd(abc)==true) {
oldabc=abc;
break;
}else if(pd(abc)==false){
System.out.println("当前字符串不符合规则,请重新输入:");
continue;
}
}
String newabc="";
for (int i = 0; i < oldabc.length(); i++) {
newabc=newabc+lmsz(oldabc.charAt(i));
newabc+=" ";
}
System.out.println(newabc);
}
public static boolean pd(String a){
if(a.length()>9){return false;}
for (int i = 0; i < a.length(); i++) {
if(a.charAt(i)<'0'||a.charAt(i)>'9'){
return false;
}
}
return true;
}
public static String lmsz(char a){
switch(a){
case '0':
return " ";
case '1':
return "Ⅰ";
case '2':
return "Ⅱ";
case '3':
return "Ⅲ";
case '4':
return "Ⅳ";
case '5':
return "Ⅴ";
case '6':
return "Ⅵ";
case '7':
return "Ⅶ";
case '8':
return "Ⅷ";
case '9':
return "Ⅸ";
default:
return "";
}
}
}