Results 1 to 10 of 10
 02212010, 10:15 PM #1Member
 Join Date
 Jan 2010
 Posts
 80
 Rep Power
 0
Convert the number to Roman numerals
Hi
I have been asked to write code that converts the number to Roman numerals.
Fortunately, I wrote the code and it works properly.
My question is
In standard Roman numerals, no more than three consecutive copies of the same letter are used.
I have tried many times to discover what the proper statement which I must write in my code is that voids using three consecutive copies of the same letter
this is the code
Java Code:import java.util.Scanner; public class RomanNumeral { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Please enter a number between 1 and 3999: "); int number = scan.nextInt(); String roman=""; while(number<=0  number>3999){ System.out.println("Invalid input. You must enter a number between 1 and 3999"); System.out.print("Please enter another number now: "); number = scan.nextInt(); } while(number>=1000){ roman += "M"; number=1000; } while(number>=900){ roman += "CM"; number=900; } while(number>=500){ roman += "D"; number=500; } while(number>=400){ roman += "CD"; number=400; } while(number>=100){ roman += "C"; number=100; } while(number>=90){ roman += "XC"; number=90; } while(number>=50){ roman += "L"; number=50; } while(number>=40){ roman += "XL"; number=40; } while(number>=10){ roman += "X"; number=10; } while(number>=9){ roman += "IX"; number=9; } while(number>=5){ roman += "V"; number=5; } while(number>=4){ roman += "IV"; number=4; } while(number>=1){ roman += "I"; number=1; } System.out.println(roman); } }
Thanks
:)

 02212010, 11:04 PM #3Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
removed......
Last edited by al_Marshy_1981; 02212010 at 11:18 PM. Reason: brain is turning to mush

 02212010, 11:17 PM #5Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
haha yes you are right I thought number was being reduced to zero for some reason in each while loop. Time for bed I think :D
 02232010, 04:26 AM #6Member
 Join Date
 Jan 2010
 Posts
 80
 Rep Power
 0
Thank u 4 response
I have another problem
The output should be the same format
Java Code:Please enter a number between 1 and 3999: 1073 0 in Roman numerals is MLXXIII
I've tried to fix this problem but I couldn't

Last edited by Fubarable; 02232010 at 05:05 AM.
 02232010, 02:57 PM #8Member
 Join Date
 Jan 2010
 Posts
 80
 Rep Power
 0
Hi Fubarable
Thank you for your passing and responsing
About the first problem, I read your response and I saw there is no problem
I just was confused by this rule
In standard Roman numerals, no more than three consecutive copies of the same letter are used.
Also the second problem, I fixed yesterday
This is code after fixing
Java Code:import java.util.Scanner; public class RomanNumeral { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Please enter a number between 1 and 3999: "); int number = scan.nextInt(); [COLOR="Red"]int number1 = number;[/COLOR] // I wrote this variable to hold the number that the user enters String roman=""; while(number<=0  number>3999){ System.out.println("Invalid input. You must enter a number between 1 and 3999"); System.out.print("Please enter another number now: "); number = scan.nextInt(); [COLOR="Red"]number1=number;[/COLOR] } while(number>=1000){ roman += "M"; number=1000; } while(number>=900){ roman += "CM"; number=900; } while(number>=500){ roman += "D"; number=500; } while(number>=400){ roman += "CD"; number=400; } while(number>=100){ roman += "C"; number=100; } while(number>=90){ roman += "XC"; number=90; } while(number>=50){ roman += "L"; number=50; } while(number>=40){ roman += "XL"; number=40; } while(number>=10){ roman += "X"; number=10; } while(number>=9){ roman += "IX"; number=9; } while(number>=5){ roman += "V"; number=5; } while(number>=4){ roman += "IV"; number=4; } while(number>=1){ roman += "I"; number=1; } System.out.println(number1 + " in Roman numerals is " + roman); } }
Thanks
 11292012, 10:43 PM #9Member
 Join Date
 Nov 2012
 Posts
 1
 Rep Power
 0
Re: Convert the number to Roman numerals
import java.util.Scanner;
public class Teste {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Type a number");
int num = 1;
String roman = "";
do{
if(sc.hasNextInt())
num = sc.nextInt();
else{
System.out.println("Error");
sc.nextLine();
sc.next();
}
}while(num<=0 && num>3999);
while(num>=900){
if(num==900){
roman+="CM";
num=0;
}
else{
roman+="M";
num=1000;
}
}
while(num>=400){
if(num==400){
roman+="CD";
num=0;
}
else{
roman+="D";
num=500;
}
}
while(num>=90){
if(num==90){
roman+="XC";
num=0;
}
else{
roman+="C";
num=100;
}
}
while(num>=40){
if(num==40){
roman+="XL";
num=0;
}
else{
roman+="L";
num=50;
}
}
while(num>=9){
if(num==9){
roman+="IX";
num=0;
}
else{
roman+="X";
num=10;
}
}
while(num>=4){
if(num==4){
roman+="IIIV";
num=0;
}
else{
roman+="V";
num=5;
}
}
while(num>=1){
roman+="I";
num=1;
}
System.out.println("Msg " +roman);
}
}
 11292012, 10:44 PM #10Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,642
 Rep Power
 7
Re: Convert the number to Roman numerals
@Rocknsock, any reason you decided to resurrect a thread that is almost 3 years old to spoonfeed code? Recommended reading:
The Problem with Spoonfeeding
Similar Threads

Convert roman numerals
By matzahboy in forum New To JavaReplies: 4Last Post: 02212010, 10:06 PM 
convert from roman numerals to integers and vice versa
By number1cynic in forum New To JavaReplies: 10Last Post: 01182010, 11:54 PM 
convert number to string
By wizard_oz in forum New To JavaReplies: 9Last Post: 11162009, 07:04 PM 
how to convert String number to int
By gabriel in forum New To JavaReplies: 5Last Post: 08022009, 03:46 PM 
Convert roman numerals to integers
By Felissa in forum Advanced JavaReplies: 2Last Post: 07012007, 11:27 PM
Bookmarks