Java 将罗马数字转换为阿拉伯语
我是Java新手,我需要编写一个程序,将罗马数字转换为阿拉伯数字 我不能使用某些函数,因为我不允许在给定代码的末尾更改参数。我需要做的一切,在公共静态无效的主要功能 我开始在谷歌上搜索并开始编码。从现在起,我只能将一个字母数字转换为X,I,V。。。阿拉伯数字,但我不能这样做,超过详细的数字席,CCC,IX,IV.…< / P> 有人能帮我吗?我对Java真的很陌生。这是我的第一种程序语言,我正在努力理解它 这是我的密码:Java 将罗马数字转换为阿拉伯语,java,roman-numerals,Java,Roman Numerals,我是Java新手,我需要编写一个程序,将罗马数字转换为阿拉伯数字 我不能使用某些函数,因为我不允许在给定代码的末尾更改参数。我需要做的一切,在公共静态无效的主要功能 我开始在谷歌上搜索并开始编码。从现在起,我只能将一个字母数字转换为X,I,V。。。阿拉伯数字,但我不能这样做,超过详细的数字席,CCC,IX,IV.…< / P> 有人能帮我吗?我对Java真的很陌生。这是我的第一种程序语言,我正在努力理解它 这是我的密码: import java.util.Scanner; class Roma
import java.util.Scanner;
class Roman {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int[] numbers = {1000, 500, 100, 50, 10, 5, 1 };
String symbols = "MDCLXVI";
/*******************************************
* Complete your program
*******************************************/
System.out.print("Enter a roman numeral");
final int MAX = 3999;
Scanner keyb = new Scanner(System.in);
String roman = keyb.next();
roman=roman.toUpperCase();
if(roman.matches(".*[0-9].*") || !roman.matches("[M|D|C|L|X|V|I]*")){
System.out.println("Impossible to convert. Wrong roman numeral");
}
int i = 0; //position in the string romain
int arabic = 0; // Arabic numeral equivalent of the part of the string that
// has been converted so far
int number;
while (i < roman.length()){
char letter = roman.charAt(i); // letter at the current position in the string
if (letter == 'I'){
number = 1;
} else if (letter == 'V'){
number = 5;
} else if (letter == 'X'){
number = 10;
} else if (letter == 'L'){
number = 50;
} else if (letter == 'C'){
number = 100;
} else if (letter == 'D'){
number = 500;
} else if (letter == 'M'){
number = 1000;
} else {
number = -1;
}
i++; // Move on to next position in the string
if (i==roman.length()){
// There is no letter in the string following the one we have just processed.
// So just add the number corresponding to the single letter to arabic.
arabic += number;
} else {
// Look at the next letter in the string. If it has a larger Roman numeral
// equivalent than number, then the two letters are counted together as
// a Roman numeral with value (nextNumber - number).
number = roman.charAt(i);
int nextNumber = number;
if(nextNumber > number){
// Combine the two letters to get one value, and move on to next position in string.
arabic += (nextNumber - number);
i++;
} else {
// Don't combine the letters. Just add the value of the one letter onto the number.
arabic += number;
}
}
System.out.println(number);
} // end while
/*******************************************
* Do not change after this line.
*******************************************/
}
}
以下是一种方法:
import java.util.Scanner;
class Roman {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int[] numbers = { 1000, 500, 100, 50, 10, 5, 1 };
String symbols = "MDCLXVI";
/*******************************************
* Complete your program
*******************************************/
System.out.print("Enter a roman numeral: ");
final int MAX = 3999;
Scanner keyb = new Scanner(System.in);
String roman = keyb.next();
keyb.close(); // don't want a resource leak
roman = roman.toUpperCase();
if (roman.matches(".*[0-9].*") || !roman.matches("[M|D|C|L|X|V|I]*")) {
System.out.println("Impossible to convert. Wrong roman numeral");
}
int i = 0; // position in the Roman string
int current = 0; // the current Roman numeral character to Arabic
// conversion
int previous = 0; // start previous at zero, that way when
// current is greater than previous in the first
// run, nothing will be subtracted from current
int arabic = 0; // Arabic numeral equivalent of the part of the string
// that has been converted so far
while (i < roman.length()) {
char letter = roman.charAt(i); // letter at the current position in
// the string
// switch statement is easier to read than if - else if - else
switch (letter) {
case ('I'):
current = 1;
break;
case ('V'):
current = 5;
break;
case ('X'):
current = 10;
break;
case ('L'):
current = 50;
break;
case ('C'):
current = 100;
break;
case ('D'):
current = 500;
break;
case ('M'):
current = 1000;
break;
}
if (current > previous) {
// subtract previous * 2 because previous was added to arabic
// once already
arabic += current - (previous * 2);
} else {
// if current is less than or equal to previous then add it to
// arabic
arabic += current;
}
previous = current; // set previous equal to current to check
// for less-than on next iteration
i += 1; // move on to next position in the string
} // end while
// print the Arabic conversion after the loop is done
System.out.println("Arabic: " + arabic);
/*******************************************
* Do not change after this line.
*******************************************/
}
}
我建议对你的单个罗马数字进行枚举。这使得代码被很好地封装
public enum Roman {
I(1), V(5), X(10), L(50), C(100), D(500), M(1000);
private final int value;
private Roman(int value) {
this.value = value;
}
public int toInt() {
return value;
}
}
将单个罗马数字转换为整数变得很简单。例如:
Roman.valueOf("X").toInt();
唯一复杂的位是处理IX和XC类型的值。识别它们的简单方法是,它们是数字不按降序排列的唯一时间。检查此项可以作为方法添加到枚举本身以继续封装:
public enum Roman {
public boolean shouldCombine(Roman next) {
return this.value < next.value;
}
public int toInt(Roman next) {
return next.value - this.value;
}
}
现在把所有这些放在一起:
List<Roman> romans = new ArrayList<>();
input.chars().mapToObj(Character::valueOf)
.map(Roman::valueOf).forEach(romans::add);
int value = 0;
while (!romans.isEmpty()) {
Roman current = romans.remove(0);
if (!romans.isEmpty() && current.shouldCombine(romans.get(0))
value += current.toInt(romans.remove(0));
else
value += current.ToInt();
}
该代码的第一部分使用Java8特性将字符串转换为罗马数字。如果您觉得有点困惑,请告诉我,我将把它转换为传统的迭代。这是正式语言的一个展望。解决此问题的唯一方法是将位置0与下一个字符进行比较。如果值小于,则从acccumulator中减去该值;如果>=,则添加该值 您最好使用switch语句,而不是if-else if-else语句。枚举的可能副本会更好,就像在sprinter answernice中一样,我有非常类似的实现。换一种方式也很有趣;
List<Roman> romans = new ArrayList<>();
input.chars().mapToObj(Character::valueOf)
.map(Roman::valueOf).forEach(romans::add);
int value = 0;
while (!romans.isEmpty()) {
Roman current = romans.remove(0);
if (!romans.isEmpty() && current.shouldCombine(romans.get(0))
value += current.toInt(romans.remove(0));
else
value += current.ToInt();
}