gpt4 book ai didi

Java - 将字符输入(表示罗马数字)转换为十进制等值

转载 作者:行者123 更新时间:2023-12-01 09:12:34 25 4
gpt4 key购买 nike

正如标题所示,我正在使用方法编写一个简单的程序,该程序将罗马数字的字符输入在(“M”,“D”,“C”,“L”,“X”,“V”之间转换) ,“I”)然后打印十进制等值。

我已经编写了程序,但它将小数(int)转换为罗马数字

当修改程序以仅接受字符输入(“M”,“D”,“C”,“L”,“X”,“V”,“I”)然后输出十进制时,我收到错误char 无法转换为 int。

关于我如何改变这一点的任何建议。谢谢

import java.util.Scanner;

class RomanNumeral {
public static String romanNumeralToInt(int romanNumeral) {
String Numeral = "";
int repeat;

int value[] = {1000, 500, 100, 50, 10, 5, 1 };
String symbol[] = {"M", "D", "C", "L", "X", "V", "I" };

for(int x = 0; romanNumeral > 0; x++) {
repeat = romanNumeral / value[x];

for(int i = 1; i <= repeat; i++) {
Numeral = Numeral + symbol[x];
}

romanNumeral = romanNumeral % value[x];
}

return Numeral;
}

public static void main(String args[]){
Scanner input = new Scanner(System.in);

final String INVALID = "Invalid number, try again!";

final int VALIDATE_NUMBER_1 = 1;
final int VALIDATE_NUMBER_5 = 5;
final int VALIDATE_NUMBER_10 = 10;
final int VALIDATE_NUMBER_50 = 50;
final int VALIDATE_NUMBER_100 = 100;
final int VALIDATE_NUMBER_500 = 500;
final int VALIDATE_NUMBER_1000 = 1000;

while (true) {
System.out.print("Enter a number: ");
int inputValue = input.nextInt();

if (inputValue == VALIDATE_NUMBER_1) {
System.out.println(VALIDATE_NUMBER_1 + " = " + romanNumeralToInt(1));
}
else if (inputValue == VALIDATE_NUMBER_5) {
System.out.println(VALIDATE_NUMBER_5 + " = " + romanNumeralToInt(5));
}
else if (inputValue == VALIDATE_NUMBER_10) {
System.out.println(VALIDATE_NUMBER_10 + " = " + romanNumeralToInt(10));
}
else if (inputValue == VALIDATE_NUMBER_50) {
System.out.println(VALIDATE_NUMBER_50 + " = " + romanNumeralToInt(50));
}
else if (inputValue == VALIDATE_NUMBER_100) {
System.out.println(VALIDATE_NUMBER_100 + " = " + romanNumeralToInt(100));
}
else if (inputValue == VALIDATE_NUMBER_500) {
System.out.println(VALIDATE_NUMBER_500 + " = " + romanNumeralToInt(500));
}
else if (inputValue == VALIDATE_NUMBER_1000) {
System.out.println(VALIDATE_NUMBER_1000 + " = " + romanNumeralToInt(1000));
}
else {
System.out.println(INVALID);
}
}

}
}

更新按照帖子的建议修改了代码,但仍然存在错误,因为 String 无法转换为 Int。有什么建议。谢谢您

import java.util.Scanner;

class RomanTest {
public static int romanNumeralToInt(char romanNumeral) {
String Numeral = "";
int repeat;

int value[] = {1000, 500, 100, 50, 10, 5, 1 };
char symbol[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I' };

for(char x = 0; romanNumeral > 0; x++) {
repeat = romanNumeral / value[x];

for(int i = 1; i <= repeat; i++) {
Numeral = Numeral + symbol[x];
}

romanNumeral = romanNumeral % value[x];
}

return Numeral;
}

public static void main(String args[]){
Scanner input = new Scanner(System.in);

final String INVALID = "Invalid number, try again!";

final char VALIDATE_CHAR_M = 'M';
final char VALIDATE_CHAR_D = 'D';
final char VALIDATE_CHAR_C = 'C';
final char VALIDATE_CHAR_L = 'L';
final char VALIDATE_CHAR_X = 'X';
final char VALIDATE_CHAR_V = 'V';
final char VALIDATE_CHAR_I = 'I';


while (true) {
System.out.print("Enter a number: ");
char inputValue = input.nextLine().charAt(0);

if (inputValue == VALIDATE_CHAR_M) {
System.out.println(VALIDATE_CHAR_M + " = " + romanNumeralToInt('M'));
}
else if (inputValue == VALIDATE_CHAR_D) {
System.out.println(VALIDATE_CHAR_D + " = " + romanNumeralToInt('D'));
}
else if (inputValue == VALIDATE_CHAR_C) {
System.out.println(VALIDATE_CHAR_C + " = " + romanNumeralToInt('C'));
}
else if (inputValue == VALIDATE_CHAR_L) {
System.out.println(VALIDATE_CHAR_L + " = " + romanNumeralToInt('L'));
}
else if (inputValue == VALIDATE_CHAR_X) {
System.out.println(VALIDATE_CHAR_X + " = " + romanNumeralToInt('X'));
}
else if (inputValue == VALIDATE_CHAR_V) {
System.out.println(VALIDATE_CHAR_V + " = " + romanNumeralToInt('V'));
}
else if (inputValue == VALIDATE_CHAR_I) {
System.out.println(VALIDATE_CHAR_I + " = " + romanNumeralToInt('I'));
}
else {
System.out.println(INVALID);
}
}

}
}

最佳答案

首先你要注意public static int romanNumeralToInt(char romanNumeral)它应该返回 int,但你返回 String Numeral = ""; - 它是 String,Java 因为 C# 是强类型语言,所以你必须返回 String。第二:以您正在做的方式连接字符串 for(int i = 1; i <= repeat; i++) {
Numeral = Numeral + symbol[x];
}
不推荐(太慢,字符串是不可变的,因此在每次连接时都创建新字符串)。更好的方法是使用 StringBuilder。

我修改了你的代码并提供了类似的内容:

private String decimalToRoman(int number) {
String[] romans = {"M", "CM", "D", "C", "XC", "L", "X", "IX", "V", "I"};
int[] values = {1000, 900, 500, 100, 90, 50, 10, 9, 5, 1};
StringBuilder builder = new StringBuilder();


for (int i = 0; i < values.length; i++) {
int times= number / values[i];

if (times== 0) {
continue;
}

if (times == 4 && i > 0) {
builder.append(romans[i]);
builder.append(romans[i - 1]);
} else {
for (int ii = 0; ii < times; ii ++) {
builder.append(romans[i]);
}
}

number = number % values[i];
}
return builder.toString();
}

关于Java - 将字符输入(表示罗马数字)转换为十进制等值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40829068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com