0

我正在编写一个将罗马数字转换为十进制数字的程序。

由于某种原因,它在检查用户输入时不返回值。然而它已经修复了,

我现在面临的是:代码没有响应我的数字(输入后一直显示空白屏幕)。

我怎样才能解决这个问题?我的代码有问题吗?我只是一个初学者,所以我学到的只是基本的东西。

public static void main(String[] args) {
        // Fill in the body
        Scanner in= new Scanner(System.in);
        String user = promptUserForNumeral(in);
        while (user.length()!=0) {
            int numb= convertNumeralToNumber(user);
            System.out.println("The numeral "+user+ " is the decimal number "+numb);
            user = promptUserForNumeral(in);
        }
    }
private static String promptUserForNumeral(Scanner inScanner) {
    // Fill in the body
    System.out.println("Enter a roman numeral (Q to quit): ");
    String i = inScanner.nextLine();
    while (i.length()<=0) {
        System.out.println("ERROR! You must enter a non-empty line!");
        System.out.println("Enter a roman numeral (Q to quit): ");
        i = inScanner.nextLine();
    }
    if ( i.equalsIgnoreCase("q")) {
        System.out.println("Goodbye!");
        System.exit(0);
    }
    return i;

}
private static int convertNumeralToNumber(String numeral) {
    // Fill in the body
    int numb = 0;
    int n=0;
    int ch=0;
    while (n<numeral.length()) {
        char l= numeral.charAt(n);
        numb=convertCharacterToNumber(l);
        if (numb<0) {
            System.out.println("Cannot be define");
            n++;
        }
        else if (n==numeral.length()) {
            ch+=numb;
        }
        else {
            int nnumb=convertCharacterToNumber(numeral.charAt(n));
            if (nnumb>numb) {
                ch+=nnumb-numb;
                n++;                    
            }
            else {
                ch+=numb;
            }
        }
    }
    if (ch>3999) {
        System.out.println("Input number must be less than 3999");
        numb=ch;
    }
    return numb;


}


private static int convertCharacterToNumber(char numeral) {
    // Fill in the body
    int n=0;
    if (numeral=='m' || numeral =='M') {
        return 1000;
    }
    else if (numeral=='d' || numeral=='D') {
        return 500; 
    }
    else if (numeral=='c' || numeral=='C') {
        return 100;
    }
    else if (numeral=='l' || numeral=='L') {
        return 50;
    }
    else if (numeral=='x' || numeral=='X') {
        return 10;
    }
    else if (numeral=='v' || numeral=='V') {
        return 5;
    }
    else if (numeral=='i' || numeral=='I') {
        return 1;
    }
    else {
        return -1;
    }

}       

}

4

4 回答 4

0
public class stringTest {
public static void main(String[] args) {
    // Fill in the body
    Scanner in= new Scanner(System.in);
    String user = promptUserForNumeral(in);
    while (user.length()!=0) {
        int numb= convertNumeralToNumber(user);
        System.out.println("The numeral "+user+ " is the decimal number "+numb);
        user = promptUserForNumeral(in);
    }
}
private static String promptUserForNumeral(Scanner inScanner) {
    // Fill in the body
    System.out.println("Enter a roman numeral (Q to quit): ");
    String i = inScanner.nextLine();
    while (i.length()>=0) {
        if (i.length()==0) {
            System.out.println("ERROR! You must enter a non-empty line!");
            System.out.println("Enter a roman numeral (Q to quit): ");
            i = inScanner.nextLine();
        }
        else if ( i.equalsIgnoreCase("q")) {
            System.out.println("Goodbye!");
            System.exit(0);
        }
        else return i; // in your program the while is never ending, so it does not return any value.
    }
    return "";
}
private static int convertNumeralToNumber(String numeral) {
    // Fill in the body
    int preNumber = 0;
    int curNumber = 0;
    int n=0;
    int ch=0;
    while (n<numeral.length()) {
        char l= numeral.charAt(n);
        curNumber=convertCharacterToNumber(l);
        if (curNumber<0) {
            System.out.println("Cannot be define");
            System.exit(0);
        }
        else {
            // I have changed the logic to evaluated decimal Number equivalent to Roman Literal
            if(preNumber < curNumber && n != 0) ch = curNumber - ch;
            else ch += curNumber;
            preNumber = curNumber;
        }
        n++;
    }
    return ch;
}


private static int convertCharacterToNumber(char numeral) {
    // Fill in the body
    if (numeral=='m' || numeral =='M') {
        return 1000;
    }
    else if (numeral=='d' || numeral=='D') {
        return 500; 
    }
    else if (numeral=='c' || numeral=='C') {
        return 100;
    }
    else if (numeral=='l' || numeral=='L') {
        return 50;
    }
    else if (numeral=='x' || numeral=='X') {
        return 10;
    }
    else if (numeral=='v' || numeral=='V') {
        return 5;
    }
    else if (numeral=='i' || numeral=='I') {
        return 1;
    }
    else {
        return -1;
    }

}    
}

您可能可以查看 promptUserForNumeral 方法,我认为没有必要。您可以将其包含在主 while 循环中以查找用户错误。

于 2014-11-04T04:49:03.920 回答
0

检查这个

while (i.length()>=0) {
    if (i.length()==0) {
        System.out.println("ERROR! You must enter a non-empty line!");
        System.out.println("Enter a roman numeral (Q to quit): ");
        i = inScanner.nextLine();
    }
    else if ( i.equalsIgnoreCase("q")) {
        System.out.println("Goodbye!");
        System.exit(0);
    }
}
return i;

当 i.length() > 0 时,这不会退出或返回任何内容。如果用户没有输入,则返回是死代码q。解决方法:用break指定一个else;那么它将起作用。

else
   break;
于 2014-11-04T04:09:14.860 回答
0

我会重写你的 while 循环:

while (i.length()<=0) {
    System.out.println("ERROR! You must enter a non-empty line!");
    System.out.println("Enter a roman numeral (Q to quit): ");
    i = inScanner.nextLine();
}
if ( i.equalsIgnoreCase("q")) {
    System.out.println("Goodbye!");
    System.exit(0);
}
return i;
于 2014-11-04T04:11:35.513 回答
0

你有很多多余的条件。问题在于这个循环:

    while (i.length() >= 0) {
        if (i.length() == 0) {
            System.out.println("ERROR! You must enter a non-empty line!");
            System.out.println("Enter a roman numeral (Q to quit): ");
            i = inScanner.nextLine();
        } else if (i.equalsIgnoreCase("q")) {
            System.out.println("Goodbye!");
            System.exit(0);
        }
    }

为我喜欢“V”取任何值。

  • 它的长度大于零,因此它进入循环。
  • 在第一个 if 条件下,它的长度再次不为零,因此它继续到 elseIf
  • 由于该值不是“q”,else 部分也不执行。
  • 所以它回到循环的开始&如果长度大于零,再次检查条件。

所以,你有一个无限循环。再次处理您的逻辑并删除任何不必要的条件。您还可以使用该break;语句终止循环。

于 2014-11-04T04:12:56.487 回答