gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:26 25 4
gpt4 key购买 nike

我需要将数值转换为罗马数字,但遇到了一些麻烦。当我测试我的代码并且用户输入一个数字时,代码将其转换为罗马数字,但之后我无法再次出现提示;它会在输入错误的整数后重新提示,但在输入正确的整数后不会重新提示。我不知道如何做到这一点,以便用户可以继续转换值,直到他们选择 -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;
}
}

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

最佳答案

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

相反,您可能想调用方法 arabicToRoman(int arabic)并简单地打印出结果。

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

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 中通过对同样的调用持续存在。这就是为什么每次调用该方法时,它都会打印当前结果并附加到先前的结果。您想要编辑代码以不存储 a作为状态变量并返回 arabicToRoman(int arabic)方法无需改变现有数据。在方法内部声明变量将允许在方法终止时释放该变量。

要进行此修复:

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

static String a = "";

然后,编辑您的 arabicToRoman(int 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;
}

我再次对此进行了测试,并可以确认它会产生您所描述的正确行为。

为了清楚起见,this is what the final product should look like .

关于java - 如何重新提示允许用户继续转换值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26826455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com