首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重新提示以允许用户继续转换值?

如何重新提示以允许用户继续转换值?
EN

Stack Overflow用户
提问于 2014-11-09 08:32:21
回答 1查看 220关注 0票数 1

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

代码语言:javascript
复制
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;
}
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-09 08:50:41

您的代码无法正常工作,因为满足return条件时调用了((number <= 3999) && (number > 0))语句。此return终止循环。

相反,您可能想调用方法arabicToRoman(int arabic),只需打印出结果。

我还会考虑重组您的代码,使其在sentinel value循环条件下使用while,并编辑代码,以便在给出非数字答案时它不会崩溃。现在编写代码时,这样做将生成一个java.util.InputMismatchException

代码语言:javascript
复制
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(); // gets the next item from the Scanner

    try {
        Integer number = Integer.parseInt(a); // tries to 'cast' the String to an Integer

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

        } 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!");
    }
}

编辑:修复连接问题

所编写的程序正在将结果存储在一个state variable中,该state variable通过对该结果的调用而持久化。这就是为什么每次调用该方法时,它都会打印附加到前一个结果的当前结果。您希望编辑代码以不将a存储为状态变量,并返回arabicToRoman(int arabic)方法而不更改现有数据。在方法中声明变量将允许在方法终止时将其释放。

为了解决这个问题:

首先,在类中的方法之外删除这一行:

代码语言:javascript
复制
static String a = "";

然后,编辑您的arabicToRoman(int arabic)方法如下所示:

代码语言:javascript
复制
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;
}

我再次测试这一点,并可以确认它产生了正确的行为,正如您所描述的。

为了清晰起见这就是最终产品的样子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26826455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档