新项目! 写一个罗马数字课。给定介于1和10之间的整数值,Roman Numeral类应该能够将此十进制值转换为罗马数字。该类还应包含一个将罗马数字转换为其十进制等效值的方法。编写第二个类(控制台或表单)来测试您的Roman Numeral类。第二类应该允许用户输入每种类型的测试值。
所以,我班上的内容是:
namespace RomanNumber
{
class RomanNumeral
{
private int numToRoman;
private string romNumber;
private string romanToNum;
public RomanNumeral()
{
}
public RomanNumeral(int numNumber)
{
numToRoman = numNumber;
}
public int NumToRoman
{
get
{
return numToRoman;
}
set
{
numToRoman = value;
}
}
public string RomanToNum
{
get
{
return romanToNum;
}
set
{
romanToNum = value;
}
}
public string ReturnRomNum()
{
string romanNum;
switch (romNumber)
{
case '1':
romanNum = "I";
break;
case '2':
romanNum = "II";
break;
case '3':
romanNum = 'III';
break;
case '4':
romanNum = 'IV';
break;
case '5':
romanNum = 'V';
case '6':
romanNum = 'VI';
break;
case '7':
romanNum = 'VII';
break;
case '8':
romanNum = 'VIII';
break;
case '9':
romanNum = 'IX';
break;
case '10':
romanNum = 'X';
break;
default:
romanNum = "Error";
break;
}
return romanNum;
}
public int ReturnNomNum()
{
int numberNum;
switch (romanToNum)
case 'I':
romanToNum = "1";
break;
case 'II':
romanToNum = "2";
break;
case 'III':
romanToNum = '3';
break;
case 'IV':
romanToNum = '4';
break;
case 'V':
romanToNum = '5';
case 'VI':
romanToNum = '6';
break;
case 'VII':
romanToNum = '7';
break;
case 'VIII':
romanToNum = '8';
break;
case 'IX':
romanToNum = '9';
break;
case 'X':
romanToNum = '10';
break;
default:
romanToNum = "Error";
break;
}
return romanToNum;
}
}
}
我已经看到了RomanNumeral类的其他示例,但它们看起来比我们在课堂上学到的任何东西都复杂得多。只是想知道我是否可以拿走我拥有的东西并在创建测试类后使其工作。请帮忙!!谢谢!
答案 0 :(得分:1)
这闻起来像家庭作业。我会伸出援助之手,用一种int to roman方法让你到达中途。
private string ToRoman(int input)
{
var lookup = new Dictionary<string, int> { {"M", 1000}, {"CM", 900}, {"D", 500}, {"CD", 400}, {"C", 100},
{"XC", 90}, {"L", 50}, {"XL", 40}, {"X", 10}, {"IX", 9}, {"V", 5}, {"IV", 4}, {"I", 1} };
var output = string.Empty;
var remainder = input;
foreach (var digit in lookup.Keys)
{
while (remainder >= lookup[digit])
{
output += digit;
remainder -= lookup[digit];
}
}
return output;
}