Java 如何重新编程以允许用户继续转换值?

Java 如何重新编程以允许用户继续转换值?,java,loops,methods,return,roman-numerals,Java,Loops,Methods,Return,Roman Numerals,我需要把一个数值转换成罗马数字,我遇到了一些麻烦。当我测试代码,用户输入一个数字时,代码会将其转换为罗马数字,但之后我无法得到再次出现的提示;它会在放置错误的整数后重新计算fine,但不会在放置正确的整数后重新计算fine。我不知道如何使它,以便用户可以继续转换值,直到他们选择-1退出。 下面是我到目前为止的想法 import java.util.Scanner; public class arabicToRoman { static String a = ""; public static

我需要把一个数值转换成罗马数字,我遇到了一些麻烦。当我测试代码,用户输入一个数字时,代码会将其转换为罗马数字,但之后我无法得到再次出现的提示;它会在放置错误的整数后重新计算fine,但不会在放置正确的整数后重新计算fine。我不知道如何使它,以便用户可以继续转换值,直到他们选择-1退出。 下面是我到目前为止的想法

import java.util.Scanner;

public class arabicToRoman {
static String a = "";

public static void main (String[] args){

    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number between 1 and 3999 (-1 to quit): ");

    while (!input.hasNext("-1")) {
        String a = input.next(); 

    try {
        Integer number = Integer.parseInt(a); 

        if ((number <= 3999) && (number > 0)) {
            System.out.println(arabicToRoman(number)); 

        } else if (number > 3999) {
            System.out.println("Error: number must be between 1 and 3999");

        } else if (number == 0) {
            System.out.println("Error: The Romans did not have a way to represent negative numbers or zero.");

        } else {
            System.out.println("Error: The Romans did not have a way to represent negative numbers or zero.");

        }
    } catch (NumberFormatException e) {
        System.out.println("You did not enter a number!");
    }
}  
}

public static String arabicToRoman(int arabic) {    
    while (arabic >= 1000){
        a += "M";
        arabic -= 1000;
    }
    while (arabic >= 900){
        a += "CM";
        arabic -= 900;
    }
    while (arabic >= 500){
        a += "D";
        arabic -= 500;
    }
    while (arabic >= 400){
        a += "CD";
        arabic -= 400;
    }
    while (arabic >= 100){
        a += "C";
        arabic -= 100;
    }
    while (arabic >= 90){
        a += "XC";
        arabic -= 90;
    }
    while (arabic >= 50){
        a += "L";
        arabic -= 50;
    }
    while (arabic >= 40){
        a += "XL";
        arabic -= 40;
    }
    while (arabic >= 10){
        a += "X";
        arabic -= 10;
    }
    while (arabic >= 9){
        a += "IX";
        arabic -= 10;
    }
    while (arabic >= 5){
        a += "V";
        arabic -= 5;
    }
    while (arabic >= 4){
        a += "IV";
        arabic -= 4;
        }
    while (arabic >= 1){
        a += "I";
        arabic -= 1;
    }
    return a;
}
}

输入一个输入后,它似乎会显示结果,但之后没有提示,用户可以输入其他内容,并将其读取;但是,如果它是另一个数字,则结果将添加到上一个结果。

由于满足数字0条件时调用了return语句,因此代码无法正常工作。此返回终止循环

相反,您可能希望调用方法arabicToRomanint arabic并简单地打印结果

<>我也会考虑重构代码,在while循环条件下使用一个前哨值,并编辑代码,这样当给定的非数字答案时,它不会崩溃。现在编写代码时,这样做将生成一个java.util.InputMismatchException

然后,编辑arabicToRomanint-arabic方法,如下所示:

public static String arabicToRoman(int arabic) {
    String a = ""; // note this String declaration at the start of the method

    while (arabic >= 1000) {
        a += "M";
        arabic -= 1000;
    }
    while (arabic >= 900) {
        a += "CM";
        arabic -= 900;
    }
    while (arabic >= 500) {
        a += "D";
        arabic -= 500;
    }
    while (arabic >= 400) {
        a += "CD";
        arabic -= 400;
    }
    while (arabic >= 100) {
        a += "C";
        arabic -= 100;
    }
    while (arabic >= 90) {
        a += "XC";
        arabic -= 90;
    }
    while (arabic >= 50) {
        a += "L";
        arabic -= 50;
    }
    while (arabic >= 40) {
        a += "XL";
        arabic -= 40;
    }
    while (arabic >= 10) {
        a += "X";
        arabic -= 10;
    }
    while (arabic >= 9) {
        a += "IX";
        arabic -= 10;
    }
    while (arabic >= 5) {
        a += "V";
        arabic -= 5;
    }
    while (arabic >= 4) {
        a += "IV";
        arabic -= 4;
    }
    while (arabic >= 1) {
        a += "I";
        arabic -= 1;
    }
    return a;
}
一、 再次测试,可以确认它产生了您所描述的正确行为


为了清晰起见,.

我尝试调用该方法并打印结果,结果成功了,但之后每个值都添加到了前一个值中。另外,在应用了您的示例之后,现在我似乎没有得到任何提示。我的观点是,在编写这些示例时,您不需要任何返回语句或System.exit0。我的例子应该有用——我刚刚测试过。确保如果你使用它,它是你的主要方法中唯一的东西。也许你应该更新你的帖子,包括你尝试过的新代码。我理解。我确实在没有返回的情况下使用System.out.printlnarabicToRomannumber尝试了它;它是有效的,但我的问题是,在那之后,任何正确的输入都被添加到上一个输入中。我用我尝试过的新代码更新了我的帖子非常感谢!!哇,我甚至没有看到我的字符串a只是在错误的地方,并导致一切工作不正常。我现在觉得自己很愚蠢。非常感谢。这是最后的解脱。你帮了我大忙,我真的很感激!
static String a = "";
public static String arabicToRoman(int arabic) {
    String a = ""; // note this String declaration at the start of the method

    while (arabic >= 1000) {
        a += "M";
        arabic -= 1000;
    }
    while (arabic >= 900) {
        a += "CM";
        arabic -= 900;
    }
    while (arabic >= 500) {
        a += "D";
        arabic -= 500;
    }
    while (arabic >= 400) {
        a += "CD";
        arabic -= 400;
    }
    while (arabic >= 100) {
        a += "C";
        arabic -= 100;
    }
    while (arabic >= 90) {
        a += "XC";
        arabic -= 90;
    }
    while (arabic >= 50) {
        a += "L";
        arabic -= 50;
    }
    while (arabic >= 40) {
        a += "XL";
        arabic -= 40;
    }
    while (arabic >= 10) {
        a += "X";
        arabic -= 10;
    }
    while (arabic >= 9) {
        a += "IX";
        arabic -= 10;
    }
    while (arabic >= 5) {
        a += "V";
        arabic -= 5;
    }
    while (arabic >= 4) {
        a += "IV";
        arabic -= 4;
    }
    while (arabic >= 1) {
        a += "I";
        arabic -= 1;
    }
    return a;
}