This java program compiles but when at the end i call System.out.println(Roman(m
ID: 3590769 • Letter: T
Question
This java program compiles but when at the end i call System.out.println(Roman(month) + Roman(day) + Roman(year)); it doesnt print out anything,
can someone please tell me whats is wrong thanks
this is my code:
import java.util.Scanner;
public class convert2Roman
{
public static void main (String[] args)
{
Scanner kb = new Scanner(System.in);
description();
start(kb);
}
public static void description()
{
System.out.println("******** This program converts your birhtday to its");
System.out.println("equivalent roman numbers ********");
}
//will accept an integer value as its parameter
//returns its roman equivilance
public static String Roman(int num)
{
int count;
String s = " ";
if (num <1 || num>3999)
{
System.out.println("Invalid number");
}
if (num >= 1000)
{
count = num/1000;
for (int a = 1; a <= count; a++)
{
s = s+"M";
}
num = num%1000;
}
else if (num >= 900)
{
count = num/900;
for (int b = 1 ;b >= count; b++)
{
s = s +"CM";
}
num =num% 900;
}
else if (num >= 500)
{
count = num/500;
for (int c = 1; c >= count ; c++)
{
s = s + "D";
}
num =num% 500;
}
else if (num >= 400)
{
count = num/400;
for (int d = 1; d >= count; d++)
{
s = s + "CD";
}
num =num% 400;
}
else if (num >= 100)
{
count = num/100;
for (int e = 1; e >=count; e++)
{
s = s + "C";
}
num =num%100;
}
else if (num >= 90)
{
count = num/90;
for (int f = 1; f >= count; f++)
{
s = s + "XC";
}
num =num%90;
}
else if (num >= 50)
{
count = num/50;
for (int g = 1; g >= count; g ++)
{
s = s + "L";
}
num =num% 50;
}
else if (num >= 40)
{
count = num/40;
for (int h = 1; h >= count; h++)
{
s = s + "XL";
}
num=num%40;
}
else if (num >= 10)
{
count = num/10;
for (int i = 1; i>= count; i++)
{
s = s + "X";
}
num =num%10;
}
else if (num >= 9)
{
count = num/9;
for (int j = 1; j>=count; j++)
{
s = s + "IX";
}
num=num%9;
}
else if (num >= 5)
{
count = num/5;
for (int k = 1; k >= count; k++)
{
s = s + "V";
}
num =num%5;
}
else if (num >= 4)
{
count = num/4;
for (int l = 1 ; l >= count; l++)
{
s = s + "IV";
}
num =num%4;
}
else if (num >= 1)
{
count = num/1;
for (int m = 1; m >= count; m++)
{
s = s + "I";
}
num =num%1;
}
return s;
}
//you need to ask user how many times he/she wants to use the app
public static void start(Scanner kb)
{
System.out.print("How many times do you wish to use this app: ");
int times = kb.nextInt();
kb.nextLine();
for (int i = 1; i<= times; i++)
{
getInfo(kb);
}
}
//Ask user for name
//ask for birthday
//get roman in this
public static void getInfo(Scanner kb)
{
//ask user for the month of birthday
System.out.print("Enter your name: ");
String name = kb.nextLine();
System.out.println("Hi "+ name +" let's start");
System.out.print("Enter the month of your birthday: ");
int month = kb.nextInt();
kb.nextLine();
System.out.print("Enter the day of your birthday: ");
int day = kb.nextInt();
kb.nextLine();
System.out.print("Enter the year of your birthday: ");
int year =kb.nextInt();
kb.nextLine();
System.out.print("Your birthday in roman is: ");
System.out.println(Roman(month) + Roman(day) + Roman(year));
}
}
Explanation / Answer
Roman.java
import java.util.Scanner;
public class convert2Roman {
public static void main (String[] args)
{
Scanner kb = new Scanner(System.in);
description();
start(kb);
}
public static void description()
{
System.out.println("******** This program converts your birhtday to its");
System.out.println("equivalent roman numbers ********");
}
//will accept an integer value as its parameter
//returns its roman equivilance
public static String Roman(int num)
{
int count;
String s = "";
if (num <1 || num>3999)
{
System.out.println("Invalid number");
}
if (num >= 1000)
{
count = num/1000;
for (int a = 1; a <= count; a++)
{
s = s+"M";
}
num = num%1000;
}
if (num >= 900)
{
count = num/900;
for (int b = 1 ;b <= count; b++)
{
s = s +"CM";
}
num =num% 900;
}
if (num >= 500)
{
count = num/500;
for (int c = 1; c <= count ; c++)
{
s = s + "D";
}
num =num% 500;
}
if (num >= 400)
{
count = num/400;
for (int d = 1; d <= count; d++)
{
s = s + "CD";
}
num =num% 400;
}
if (num >= 100)
{
count = num/100;
for (int e = 1; e <=count; e++)
{
s = s + "C";
}
num =num%100;
}
if (num >= 90)
{
count = num/90;
for (int f = 1; f <= count; f++)
{
s = s + "XC";
}
num =num%90;
}
if (num >= 50)
{
count = num/50;
for (int g = 1; g <= count; g ++)
{
s = s + "L";
}
num =num% 50;
}
if (num >= 40)
{
count = num/40;
for (int h = 1; h <= count; h++)
{
s = s + "XL";
}
num=num%40;
}
if (num >= 10)
{
count = num/10;
for (int i = 1; i<= count; i++)
{
s = s + "X";
}
num =num%10;
}
if (num >= 9)
{
count = num/9;
for (int j = 1; j<=count; j++)
{
s = s + "IX";
}
num=num%9;
}
if (num >= 5)
{
count = num/5;
for(int k = 1; k <= count; k++)
{
s = s + "V";
}
num =num%5;
}
if (num >= 4)
{
count = num/4;
for (int l = 1 ; l <= count; l++)
{
s = s + "IV";
}
num =num%4;
}
if (num >= 1)
{
count = num/1;
for (int m = 1; m <= count; m++)
{
s = s + "I";
}
num =num%1;
}
return s;
}
//you need to ask user how many times he/she wants to use the app
public static void start(Scanner kb)
{
System.out.print("How many times do you wish to use this app: ");
int times = kb.nextInt();
kb.nextLine();
for (int i = 1; i<= times; i++)
{
getInfo(kb);
}
}
//Ask user for name
//ask for birthday
//get roman in this
public static void getInfo(Scanner kb)
{
//ask user for the month of birthday
System.out.print("Enter your name: ");
String name = kb.nextLine();
System.out.println("Hi "+ name +" let's start");
System.out.print("Enter the month of your birthday: ");
int month = kb.nextInt();
kb.nextLine();
System.out.print("Enter the day of your birthday: ");
int day = kb.nextInt();
kb.nextLine();
System.out.print("Enter the year of your birthday: ");
int year =kb.nextInt();
kb.nextLine();
System.out.print("Your birthday in roman is: ");
System.out.println(Roman(month)+" : " + Roman(day)+" : " + Roman(year));
}
}
Issue in the given code:
For loop inside if conditions are going infinite as (>=) is being used.
Else If ladder wont solve the problem here, so need to use normal if conditions.
kindly let me know if u need more details
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.