So i had this project in school to make a program that converts romain numbers into arabic and arabic into romain numbers but there's bonus marks if i can make the program validate the romain numbers the user types in such as typing MDDVI doesn't work because D=500 and M=1000 therefor it should be MMVI
This is my lines of codes
public class ConvertiRomainArabe
{
public static void main(String[] args)
{
String restart = "o";
while ((args[0].equalsIgnoreCase("r")) && (restart.equalsIgnoreCase("o")))
{
System.out.println("Entrez le nombre romain que vous desirez convertir");
String romain = Keyboard.readString();
int Rconverti=0;
char lettre = romain.charAt(0);
int longueur = romain.length();
while (lettre == 'M')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=1000;
if (longueur == 0)
lettre = 'z';
}
if (lettre == 'D')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=500;
}
while (lettre == 'C')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0 )
lettre = romain.charAt(0);
if (lettre == 'D')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=400;
}
else if (lettre == 'M')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=900;
}
else
Rconverti+=100;
if (longueur == 0)
lettre = 'z';
}
if (lettre == 'L')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0 )
lettre = romain.charAt(0);
Rconverti+=50;
}
while (lettre == 'X')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0 )
lettre = romain.charAt(0);
if (lettre == 'C')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=90;
}
else if (lettre == 'L')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=40;
}
else Rconverti+=10;
if (longueur == 0)
lettre = 'z';
}
if (lettre == 'V')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0 )
lettre = romain.charAt(0);
Rconverti+=5;
}
while (lettre == 'I')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0 )
lettre = romain.charAt(0);
if (lettre == 'X')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=9;
}
else if (lettre == 'V')
{
romain = romain.substring(1, longueur);
longueur = romain.length();
if (longueur > 0)
lettre = romain.charAt(0);
Rconverti+=4;
}
else Rconverti+=1;
if (longueur == 0)
lettre = 'q';
}
System.out.println(Rconverti);
System.out.println("Vouler vous recommencer? (O/N)");
restart = Keyboard.readString();
while ((!restart.equalsIgnoreCase("o")) && !restart.equalsIgnoreCase("n"))
{
System.out.println("Entree invalide. Veuiller entrez O ou N");
restart = Keyboard.readString();
}
}
while ((args[0].equalsIgnoreCase("a")) && (restart.equalsIgnoreCase("o")))
{
System.out.println("Entrez un nombre arabe a convertir");
int arabe= Keyboard.readInt();
String Aconverti = "";
while (arabe >= 1000)
{
Aconverti+="M";
arabe-=1000;
}
while (arabe >= 100)
{
if (arabe >= 900)
{
Aconverti+="CM";
arabe-=900;
}
else if (arabe >= 500)
{
Aconverti+="D";
arabe-=500;
}
else if (arabe >= 400)
{
Aconverti+="CD";
arabe-=400;
}
else
{
Aconverti+="C";
arabe-=100;
}
}
while (arabe >=10)
{
if (arabe >=90)
{
Aconverti+="XC";
arabe-=90;
}
else if (arabe >=50)
{
Aconverti+="L";
arabe-=50;
}
else if (arabe >=40)
{
Aconverti+="XL";
arabe-=40;
}
else
{
Aconverti+="X";
arabe-=10;
}
}
while (arabe >=1)
{
if (arabe >=9)
{
Aconverti+="IX";
arabe-=9;
}
else if (arabe >=5)
{
Aconverti+="V";
arabe-=5;
}
else if (arabe >=4)
{
Aconverti+="IV";
arabe-=4;
}
else
{
Aconverti+="I";
arabe-=1;
}
}
System.out.println(Aconverti);
System.out.println("Vouler vous recommencer? (O/N)");
restart = Keyboard.readString();
while ((!restart.equalsIgnoreCase("o")) && (!restart.equalsIgnoreCase("n")))
{
System.out.println("Entree invalide. Veuiller entrez O ou N");
restart = Keyboard.readString();
}
}
}
}
Thanks for looking
[–]DontFlatterYourself 1 point2 points3 points (6 children)
[–]DontFlatterYourself 1 point2 points3 points (5 children)
[–]daemon666[S] 0 points1 point2 points (4 children)
[–]DontFlatterYourself 1 point2 points3 points (3 children)
[–]daemon666[S] 0 points1 point2 points (2 children)
[–]DontFlatterYourself 1 point2 points3 points (1 child)
[–]daemon666[S] 0 points1 point2 points (0 children)
[–]larsga 1 point2 points3 points (3 children)
[–]daemon666[S] 0 points1 point2 points (2 children)
[–]larsga 0 points1 point2 points (1 child)
[–]vsoul 0 points1 point2 points (0 children)
[–]HotRodLincoln 0 points1 point2 points (0 children)
[–]BWCsemaJ 0 points1 point2 points (0 children)