0

我需要使用不同的方法将数值转换为罗马数值。下面是我已经创建的代码。

public static void main (String[]args){
    Scanner in = new Scanner(System.in);
    int input = in.nextInt();
    String s = "";
}
private static int promptUserForNumber(Scanner inScanner) {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = inScanner.nextInt();
    if((input < 1 && input > 3999) && input != 0){
        System.out.println("Error, number must be between 1 and 3999");
         if (input == 0){
            System.out.println("Goodbye");
         }  
        }
    return input;
}
public static String convertNumberToNumeral(int input) {    
    String s = "";
    Scanner in = new Scanner(System.in);
    while (input >= 1000){
        s += "M";
        input -= 1000;
    }
    while (input >= 900){
        s += "CM";
        input -= 900;
    }
    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    while (input >= 100){
        s += "C";
        input -= 100;
    }
    while (input >= 90){
        s += "XC";
        input -= 90;
    }
    while (input >= 50){
        s += "L";
        input -= 50;
    }
    while (input >= 40){
        s += "XL";
        input -= 40;
    }
    while (input >= 10){
        s += "X";
        input -= 10;
    }
    while (input >= 9){
        s += "IX";
        input -= 10;
    }
    while (input >= 5){
        s += "V";
        input -= 5;
    }
    while (input >= 4){
        s += "IV";
        input -= 4;
        }
    while (input >= 1){
        s += "I";
        input -= 1;
    }
    }
    return s;
}

截至目前有一个无限循环,我不知道我哪里出错了。输出应该是输入一个介于 0 和 3999 之间的数值,然后在下一行它应该输出罗马数字。谢谢!

4

3 回答 3

1

我不知道你是否留下了一些代码,但这段代码不会带你到任何地方,因为 main 方法没有调用其他方法。

您想要做的是调用可以通过这种方式完成的其他方法:

System.out.println(convertNumberToNumeral(promptUserForNumber(in)));

立即打印出答案。在测试你的代码时,我发现了另一个问题,你的程序没有正确显示数字 3999 和 2999。问题是这样的:

    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    } // You forgot this curly bracket and you have 
      // to delete one curly bracket at the end of your method
    while (input >= 100){
        s += "C";
        input -= 100;
    }

您的代码也没有很好地编写脚本。有很多未使用的代码。我把它改成这样:

public class RomanNumbers{
static String s = "";
static Scanner scan;

public static void main (String[] args){
    scan = new Scanner(System.in);
    System.out.println(convertNumberToNumeral(promptUserForNumber()));
}

private static int promptUserForNumber() {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = scan.nextInt();

    if(input < 0 && input > 3999){
        System.out.println("Error, number must be between 1 and 3999");
    } else if(input == 0) {
        System.out.println("Goodbye");
    }
    return input;
}

public static String convertNumberToNumeral(int input) {    
    while (input >= 1000){
        s += "M";
        input -= 1000;
    }
    while (input >= 900){
        s += "CM";
        input -= 900;
    }
    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    }
    while (input >= 100){
        s += "C";
        input -= 100;
    }
    while (input >= 90){
        s += "XC";
        input -= 90;
    }
    while (input >= 50){
        s += "L";
        input -= 50;
    }
    while (input >= 40){
        s += "XL";
        input -= 40;
    }
    while (input >= 10){
        s += "X";
        input -= 10;
    }
    while (input >= 9){
        s += "IX";
        input -= 10;
    }
    while (input >= 5){
        s += "V";
        input -= 5;
    }
    while (input >= 4){
        s += "IV";
        input -= 4;
        }
    while (input >= 1){
        s += "I";
        input -= 1;
    }
    return s;
}
}
于 2014-10-25T19:18:39.397 回答
1
    while (input >= 400){
    s += "CD";
    input -= 400;

} 这里你需要一个“}”

于 2014-10-25T19:09:28.437 回答
0
public static void main (String[]args){
    Scanner in = new Scanner(System.in);
    int i = promptUserForNumber(in);
    if (i != -1) {
        //convertNumberToNumeral
    } else {
        //invalid no is entered
    }
}
private static int promptUserForNumber(Scanner inScanner) {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = inScanner.nextInt();
    if((input < 1 && input > 3999) || input == 0) {
        System.out.println("Error, number must be between 1 and 3999");
         return -1;
    }
    return input;
}

}另外,正如吉米已经指出的那样,您缺少一个。

于 2014-10-25T19:13:54.203 回答