如何将一个类分为两个类

时间:2016-03-02 02:45:43

标签: java

我完成了一个程序,它是一个介于1到3999之间的数字,并将其更改为罗马数字,我让它工作但我必须将它放在两个类中,一个主类和一个测试器类我将如何做到这一点?我知道这可能是一个非常简单的问题,但我似乎无法弄明白如何将它们分成主类和测试类。

public static void main(String[] args)     
{
    Scanner scan = new Scanner(System.in);
    System.out.println("Welcome to integer to Roman numeral conversion program ");
    System.out.println("------------------------------------------------------ ");
    System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
    int number= scan.nextInt();
    String numberString="";
    if (number<=1||number >3999)
    {
        System.out.println("Sorry, the number is outside the range. Good bye!");
        System.exit(0);
    }
    switch ((number%10000)/1000)  
    {
        case 1:  numberString += "M";
            break;
        case 2:  numberString += "MM";
             break;
        case 3:  numberString += "MMM";
             break;
    }
    switch ((number%1000)/100) 
    {
        case 1:  numberString += "C";
             break;
        case 2:  numberString += "CC";
             break;
        case 3:  numberString += "CCC";
             break;
        case 4:  numberString += "CD";
             break;
        case 5:  numberString += "D";
              break;
        case 6:  numberString += "DC";
              break;
        case 7:  numberString += "DCC";
              break;
        case 8:  numberString += "DCCC";
             break;
        case 9:  numberString += "CM";
             break;
    }
    switch ((number%100)/10) 
    {
        case 1:  numberString += "X";
             break;
        case 2:  numberString += "XX";
             break;
        case 3:  numberString += "XXX";
             break;
        case 4:  numberString += "XL";
             break;
        case 5:  numberString += "L";
             break;
        case 6:  numberString += "LX";
             break;
        case 7:  numberString += "LXX";
             break;
        case 8:  numberString += "LXXX";
             break;
        case 9:  numberString += "XC";
             break;
    }
    switch (number%10) 
    {
        case 1:  numberString += "I";
             break;
        case 2:  numberString += "II";
             break;
        case 3:  numberString += "III";
             break;
        case 4:  numberString += "IV";
             break;
        case 5:  numberString += "V";
             break;
        case 6:  numberString += "VI";
             break;
        case 7:  numberString += "VII";
             break;
        case 8:  numberString += "VIII";
             break;
        case 9:  numberString += "IX";
             break;
    }

    System.out.println(number + " in Roman numerals is " + numberString);
    System.out.println("Thanks for using my program. Good bye!");
    System.exit(0);
}

1 个答案:

答案 0 :(得分:0)

@Vikrant Kashyap指出。您可以将其分解为测试类和转换类。我没有机会编译代码。让我知道这是否有效。

RomanNumeralsTest.java

public class RomanNumeralsTest
{
    public static void main(String[] args)     
    {
        Scanner scan = new Scanner(System.in);
        RomanNumerals rn = new RomanNumerals();
        System.out.println("Welcome to integer to Roman numeral conversion program ");
        System.out.println("------------------------------------------------------ ");
        System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
        int number= scan.nextInt();
        if (number<=1||number >3999)
        {
            System.out.println("Sorry, the number is outside the range. Good bye!");
            System.exit(0);
        }

        System.out.println(number + " in Roman numerals is " + rn.convertToRomanNumeral(number));
        System.out.println("Thanks for using my program. Good bye!");
        System.exit(0);
    }

}

RomanNumerals.java

public class RomanNumerals
{
    public String convertToRomanNumeral(int number)
    {
        String numberString = "";
        switch ((number%10000)/1000)  
        {
            case 1:  numberString += "M";
                break;
            case 2:  numberString += "MM";
                 break;
            case 3:  numberString += "MMM";
                 break;
        }
        switch ((number%1000)/100) 
        {
            case 1:  numberString += "C";
                 break;
            case 2:  numberString += "CC";
                 break;
            case 3:  numberString += "CCC";
                 break;
            case 4:  numberString += "CD";
                 break;
            case 5:  numberString += "D";
                  break;
            case 6:  numberString += "DC";
                  break;
            case 7:  numberString += "DCC";
                  break;
            case 8:  numberString += "DCCC";
                 break;
            case 9:  numberString += "CM";
                 break;
        }
        switch ((number%100)/10) 
        {
            case 1:  numberString += "X";
                 break;
            case 2:  numberString += "XX";
                 break;
            case 3:  numberString += "XXX";
                 break;
            case 4:  numberString += "XL";
                 break;
            case 5:  numberString += "L";
                 break;
            case 6:  numberString += "LX";
                 break;
            case 7:  numberString += "LXX";
                 break;
            case 8:  numberString += "LXXX";
                 break;
            case 9:  numberString += "XC";
                 break;
        }
        switch (number%10) 
        {
            case 1:  numberString += "I";
                 break;
            case 2:  numberString += "II";
                 break;
            case 3:  numberString += "III";
                 break;
            case 4:  numberString += "IV";
                 break;
            case 5:  numberString += "V";
                 break;
            case 6:  numberString += "VI";
                 break;
            case 7:  numberString += "VII";
                 break;
            case 8:  numberString += "VIII";
                 break;
            case 9:  numberString += "IX";
                 break;
        }
        return numberString;
    }       
}