编写程序将int转换为罗马数字,但我有点卡住:

时间:2018-03-10 01:27:51

标签: java

我正在为编程课程的第一篇介绍编写这个程序,所以我对编码仍然很陌生。这是我到目前为止转换类中的内容:

/**
 * Write a description of class Conversion here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Conversion
{
    // instance variables - replace the example below with your own
    private int num;
    private String Roman;
    /**
     * Constructor for objects of class Conversion
     */
    public Conversion(int num, String Roman)
    {
        // initialise instance variables
        num = num;
        Roman = Roman;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public static String convert(int num)
    {
        // put your code here
        String Roman= " ";
        num = num;
        int thousand = num / 1000;
        int hundred = num / 100;
        int ten = num / 10;



        switch (thousand) {
            case 1: Roman += "M";
            break;
            case 2: Roman += "MM";
            break;
            case 3: Roman += "MM";
            break;
            default: Roman += "Invalid";
            break;


        }


        switch (hundred) {
            case 1: Roman += "C";
            break;
            case 2: Roman += "CC";
            break;
            case 3: Roman += "CCC";
            break;
            case 4: Roman += "CD";
            break;
            case 5: Roman += "D";
            break;
            case 6: Roman += "DC";
            break;
            case 7: Roman += "DCC";
            break;
            case 8: Roman += "DCCC";
            break;
            case 9: Roman += "CM";
            break;
            default: Roman += "Invalid";
            break;


        }

        switch (ten) {
            case 1: Roman += "X";
            break;
            case 2: Roman += "XX";
            break;
            case 3: Roman += "XXX";
            break;
            case 4: Roman += "XL";
            break;
            case 5: Roman += "L";
            break;
            case 6: Roman += "LX";
            break;
            case 7: Roman += "LXX";
            break;
            case 8: Roman += "LXXX";
            break;
            case 9: Roman += "XC";
            break;
            default: Roman += "Invalid";
            break;

        }

        switch (num) {
            case 1: Roman += "I";
            break;
            case 2: Roman += "II";
            break;
            case 3: Roman += "III";
            break;
            case 4: Roman += "IV";
            break;
            case 5: Roman += "V";
            break;
            case 6: Roman += "VI";
            break;
            case 7: Roman += "VII";
            break;
            case 8: Roman += "VIII";
            break;
            case 9: Roman += "IX";
            break;
            default: Roman += "Invalid";
            break;

        }
        return Roman;    
    }   



}

This is my Scanner class to see if the code functions:

    import java.util.Scanner;
/**
 * Write a description of class Lab7 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Lab7
{
    // instance variables - replace the example below with your own
    private int num;

    /**
     * Constructor for objects of class Lab7
     */
    public Lab7(int num)
    {
        // initialise instance variables
        this.num = num;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public static void main(String args[])
    {
        // put your code here
        Scanner Roman = new Scanner(System.in);
        System.out.println("Welcome to integer to Roman numeral conversion program");
        System.out.println("------------------------------------------------------");
        System.out.println("Please enter an integer in the range 1-3999 (both inclusive): ");
        int num = Roman.nextInt();

        do {
            System.out.println(num + " In Roman numerals is: " + Conversion.convert(num));
        }while (num <= 3999 && num >= 1);
    }
}

当我运行代码时,我得到一个无限循环,继续打印......罗马数字是:MInvalid

我很困难,不知道下一步该去哪儿。感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:2)

您应该尝试使用调试器,或者只打印变量。例如,当您的switch(hundred)向字符串追加'无效'时,这意味着hundred不是数字1-9,那么它是什么?您需要在convert方法中执行其他操作。

循环是无限的,因为,你不会在循环中更改num的值,并查看循环条件。就像一个猜测,如果没有你的确切赋值规范,我会期望main中的循环对于像这样的简单程序会执行如下操作:

do {
    // take user input
} while (/* input is invalid */);

编辑:有时循环也是这样的:

do {
    // take user input
    // do something and print some output
} while (/* input is not the special value which should exit */);

答案 1 :(得分:-1)

几乎没有变化: -

  1. “Conversion”构造函数: - “this”关键字用于引用当前的类实例变量。

    this.num = num
    this.Roman = Roman;
    
  2. 转换方法: -  在最后一个Switch循环中包含新变量“ones”以保留最后一位而不是“num”。

    int thousand = num / 1000;
    int hundred = (num%1000) / 100;
    int ten = (num % 100)/10;
    int ones = num%10;
    
  3. 主要方法中的
  4. do-while循环: - 你的条件完全相反。更改为: -

    while (num >= 3999 && num <= 1); 
    
  5. 希望这会有所帮助:)