jeudi 4 février 2016

for loop or if statements? Or something better?

I'm kinda new to programming and I struggle with java. I have been working on this problem and I can't figure out the best way to work it out using recursion. I have tried if statements but I think I need to use a for loop as the lecturer said that recursion would work here.

I can't use an array as the datatypes are not the same. I am very confused as to how to solve this problem. I've abandoned the if statements and am trying a for loop now. If someone could give me a nudge in a better direction , that would be great!

The problem is as follows: You must create a Java program to convert a
Roman Numeral value to Decimal or vice versa.

Assignment of Decimal numbers to Roman Numerals: 1=I, 4=IV, 5=V, 9=IX, 10=X, 40=XL, 50=L, 90=XC, 100=C, 400=CD, 500=D, 900=CM, 1000=M.

My program so far:

RomanNumerals.java

  public class RomanNumerals {

      public static void main(String args[]){

      Utilities myUtilities = new Utilities();

      myUtilities.userChoice();

      }
  }

  Utilities.java

  import java.util.Scanner;

  public class Utilities {

   private String romanNum;
   private double decimalNum;
   private int userChoice;
   private double remainder;

   //constructor
   public Utilities(){
   }

   StringBuffer sb = new StringBuffer();

   public void convertDecToRom()
   {

   System.out.println("Enter the number to convert to a roman numberal: ");
   Scanner dInput = new Scanner(System.in);
   decimalNum = dInput.nextDouble();

   double remainder;

 for (decimalNum <= 1000 || decimalNum >=900; remainder = remainder - 1000;)

   //if(decimalNum <= 1000 || decimalNum >= 900 )
   {
       sb.append ("M");
       remainder = decimalNum - 1000;

   }

   if (remainder <900 || remainder >500)
   {
       sb.append("CM");
       remainder = remainder - 900;
   }

   if (remainder < 500 || remainder >= 400)
   {
       sb.append("D");
       remainder = remainder - 500;
   }

   if (remainder < 400 || remainder >= 100)
   {
       sb.append("CD");
       remainder = remainder - 400;
   }

   if (remainder < 100 || remainder >= 90)
   {
       sb.append("C");
       remainder = remainder - 100;
   }
   if (remainder < 90 || remainder >= 50)
   {
       sb.append("XL");
       remainder = remainder - 90;
   }

   if (remainder < 50 || remainder >= 40)
   {
       sb.append("L");
       remainder = remainder - 50;
   }
      if (remainder < 40 || remainder <= 10)
   {
       sb.append("XL");
       remainder = remainder - 40;
   }

   if (remainder < 10 || remainder <= 9)
   {
       sb.append("X");
       remainder = remainder - 10;
   }

   if (remainder < 9 || remainder >= 5)
   {
       sb.append("IX");
       remainder = remainder - 9;
   }

   if (remainder < 5 || remainder >=  4)
   {
       sb.append("V");
       remainder = remainder - 5;
   }
   if (remainder < 4 || remainder >= 1)
   {
       sb.append("I");
   }

   System.out.println(sb);

   }



public void convertRomToDec()
{

System.out.print("Enter the roman numeral to convert to a number:");
Scanner nInput = new Scanner(System.in);
romanNum = nInput.next();

}

public void userChoice() {

do{
    Utilities myUtilities = new Utilities();

System.out.println("Choose 1 to convert a decimal number to a roman numeral or choose 2  to convert a roman numeral to a decimal: ");
Scanner inuptChoice = new Scanner(System.in);
userChoice = inuptChoice.nextInt();

   //do{
       switch (userChoice) {
           case 1:
               myUtilities.convertDecToRom();
               break;
           case 2:
               myUtilities.convertRomToDec();
               break;
           default:
               System.out.println("Not a valid choice!");
               break;
       }
   }while (!(userChoice == 1 || userChoice == 2));

   }  
   }

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire