简体   繁体   中英

Integer to Roman Numeral. I dont understand how to call the methods back to main. i am stuck here and need some assistance

public static void main(String[] args) {




// Given a Scanner as input, prompts the user to input a number between 1 and 3999.
// Checks to make sure the number is within range, and provides an error message until
// the user provides a value within range.  Returns the number input by the user to the
// calling program.
private static int promptUserForNumber(Scanner inScanner) {
    Scanner Keyboard = new Scanner(System.in);
    System.out.print("Please enter a number between 1 and 3999 (0 to quit): ");
    int number = Keyboard.nextInt();
    int number1 = number; 

    String roman="";


    while(number<=0 || number>3999){
        System.out.println("ERROR!  Number must be between 1 and 3999");
        number = Keyboard.nextInt();
        number1=number;
        }

}

private static convertNumberToNumeral(int number) {
    while(number>=1000){
        roman += "M";
        number-=1000;
    }

    while(number>=900){
        roman += "CM";
        number-=900;
    }

    while(number>=500){
        roman += "D";
        number-=500;
    }

    while(number>=400){
        roman += "CD";
        number-=400;
    }

    while(number>=100){
        roman += "C";
        number-=100;
    }

    while(number>=90){
        roman += "XC";
        number-=90;
    }

    while(number>=50){
        roman += "L";
        number-=50;
    }

    while(number>=40){
        roman += "XL";
        number-=40;
    }

    while(number>=10){
        roman += "X";
        number-=10;
    }

    while(number>=9){
        roman += "IX";
        number-=9;
    }

    while(number>=5){
        roman += "V";
        number-=5;
    }

    while(number>=4){
        roman += "IV";
        number-=4;
    }

    while(number>=1){
        roman += "I";
        number-=1;
    }

    System.out.println(number1 + " in Roman numerals is " + roman);
}
}

// Given a digit and the Roman numerals to use for the "one", "five" and "ten" positions,
// returns the appropriate Roman numeral for that digit.  For example, if the number to
// convert is 49 we would call convertDigitToNumeral twice.  The first call would be:
//     convertDigitToNumeral(9, 'I','V','X')
// and would return a value of "IX".  The second call would be:
//     convertDigitToNumeral(4, 'X','L','C')
// and would return a value of "XL".  Putting those together we would see that 49 would be the
// Roman numeral XLIX.
// Call this method from convertNumberToNumeral above to convert an entire number into a 
// Roman numeral.
private static convertDigitToNumeral(int digit, char one, char five, char ten) {
    // Fill in the body
}

} I have to do a integer to Roman Numeral program by following these comments.I am following the methods, but i don't know how to send things back to main and i don't understand the private static convertDigitToNumeral vs. convertNumberToNumeral. I am really confused.

The short answer is yes, you have to put something in the main method, even if it is something as short as calling the other methods. Most will advocate having as little as possible in the main method and put most of your program in other methods, which is how this appears to be set up. From the description you have given it looks like you will want to call promptUserForNumber right away. Once they have entered something, then check that number to see if it falls within the guidelines. This is something you will probably want to write an additional method to accomplish. Once the entry is verified you can call the remainder of the methods to perform the intended purpose of the program. Good Luck!

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