简体   繁体   中英

How do I reprompt to allow user to continue converting values?

I need to convert a numeric value to roman numerals and I'm having some trouble. When I test my code and a user inputs a number, the code converts it to a roman numeral, but I can't get the prompt to appear again afterwards; it reprompts fine after an incorrect integer is placed, but not after a correct one. I'm not sure how to make it so that the user can continue converting values until they choose -1 to quit. Below is what I have come up with thus far.

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;
}
}

After entering an input, it seems to display the result, but there is no prompt afterwards, the user can however input something else and it will read it; if it's another number however the result is added to the previous result.

Your code is not working correctly because of the return statement called when the ((number <= 3999) && (number > 0)) condition is met. This return terminates the loop.

Instead, you probably want to call the method arabicToRoman(int arabic) and simply print out the result.

I would also consider restructuring your code to use a sentinel value in the while loop condition and editing the code so that it does not crash when a non-number answer given. As your code is written now, doing such would generate a 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!");
    }
}

Edit: Fixing the concatenation problem

The program as it was written was storing the result in a state variable that persisted through the calls to the same. This is why every time the method was called, it printed the current result appended to the previous result(s). You want to edit the code to not store a as a state variable and return the arabicToRoman(int arabic) method without mutating existing data. Declaring the variable inside the method would allow it to be disposed of when the method terminates.

To make this fix:

First, remove this line outside of your methods in the class:

static String a = "";

Then, edit your arabicToRoman(int arabic) method to look like this:

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;
}

I, again, tested this and can confirm it produces the correct behavior as your described.

For the sake of clarity, this is what the final product should look like .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM