java - 在 Java 中将整数转换为罗马数字

标签 java variables roman-numerals

我的 Java 类的作业指示我们获取用户输入的整数(最多 3999)并将其转换为罗马数字。我在代码中遇到的问题是,当我在测试时输入整数时,它将整数设置为 0 并且罗马数字打印为 null。这可能是在对象内部声明变量的结果,但目前我不确定。

public class RomanNumerals
{

    // instance variables

    int input;
    String romanNum;

    /**
     * Object that records the input from a user.
     */
    public RomanNumerals()
    {
        int input=0;
        String romanNum="";

        //System.out.println("Input a number to get a Roman Numeral");
        //int input = integer.nextInt();
    }
    public int Scanner()
    {
        Scanner integer = new Scanner(System.in);
        System.out.println("Input a number to get a Roman Numeral: ");
        int input = integer.nextInt();
        return input;
    }
    /**
     * Object that takes the input from the user and determines what roman numerals are appropriate.
     */
    public String Int2Roman()
    {
        if (input < 1 || input > 3999)
        {
            System.out.println("Please input a number between 1 and 3999.");
        }
        while (input >= 1000)
        {
            romanNum = romanNum + "M";
            input = input - 1000;
        }
        while (input >= 900)
        {
            romanNum = romanNum + "CM";
            input = input - 900;
        }
        while (input >= 500)
        {
            romanNum = romanNum + "S";
            input = input - 500;
        }
        while (input >= 400)
        {
            romanNum = romanNum + "CS";
            input = input - 1000;
        }
        while (input >= 100)
        {
            romanNum = romanNum + "C";
            input = input - 100;
        }
        while (input >= 90)
        {
            romanNum = romanNum + "XC";
            input = input - 1000;
        }
        while (input >= 50)
        {
            romanNum = romanNum + "L";
            input = input - 1000;
        }
        while (input >= 40)
        {
            romanNum = romanNum + "XL";
            input = input - 40;
        }
        while (input >= 10)
        {
            romanNum = romanNum + "X";
            input = input - 10;
        }
        while (input >= 9)
        {
            romanNum = romanNum + "IX";
            input = input - 9;
        }
        while (input >= 5)
        {
            romanNum = romanNum + "V";
            input = input - 5;
        }
        while (input >= 4)
        {
            romanNum = romanNum + "IV";
            input = input - 4;
        }
        while (input >= 1)
        {
            romanNum = romanNum + "I";
            input = input - 5;
        }
        System.out.println("The Roman Numeral of " + input + " is " + romanNum);
        return romanNum;
    }
    public static void main(String[] args)
    {
        RomanNumerals a = new RomanNumerals();
        a.Scanner();
        a.Int2Roman();
    }
}

最佳答案

您的Scanner方法不会将键盘输入存储在字段(实例变量)input中,而是存储在本地变量input中。

替换

int input = integer.nextInt(); //this declares a new local variable

input = integer.nextInt(); //uses the existing field 

关于java - 在 Java 中将整数转换为罗马数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60779420/

相关文章:

r - 将罗马数字转换为R中的数字

java - C/C++ 代码与服务器 (tomcat) 上的远程 Web 服务通信

java - Squirrel SQL 在哪里存储其自动更正条目?

jquery - jQuery $(this) 是否需要缓存

javascript - 这些迭代有什么不同?

java - 我如何处理将阿拉伯数字转换为罗马数字的家庭作业?

java - 什么是<? super T> 语法?

java - 使用 Commons Net FTPSClient 连接到 FTP 服务器时出现 "Remote host closed connection during handshake"

c++在该类中声明该类的变量

php - 如何对一组罗马数字进行排序?