2014-11-09 57 views
1

我需要將數值轉換爲羅馬數字,我遇到了一些麻煩。當我測試我的代碼並且用戶輸入一個數字時,代碼會將其轉換爲羅馬數字,但是我無法在提示後再次出現;在放置了錯誤的整數後它會重新提示,但不會在正確的整數之後重新提示。我不知道如何製作,以便用戶可以繼續轉換值,直到他們選擇-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; 
} 
} 

進入輸入後,它似乎顯示結果,但沒有提示後,用戶可以輸入但是別的東西,它會讀它;如果它是另一個數字,則結果會添加到前一個結果中。

回答

0

由於在滿足((number <= 3999) && (number > 0))條件時調用return語句,您的代碼無法正常工作。這return終止循環。

取而代之,您可能需要調用方法arabicToRoman(int arabic)並簡單地打印出結果。

我還會考慮重構您的代碼,以便在while循環條件中使用sentinel value並編輯代碼,以便在給出非數字答案時不會崩潰。由於您的代碼是現在編寫的,因此會生成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

+0

我試着調用這個方法並打印出結果並且工作,但之後每個值都被添加到前一個值中。另外,在應用你的例子之後,現在我似乎沒有得到任何提示。 – User121x 2014-11-09 09:10:24

+0

我的觀點是,在寫這些時,你不需要任何'return'語句或'System.exit(0)'。我的例子應該工作 - 我只是測試它。確保如果你使用它,它是你主要方法中唯一的東西。也許你應該更新你的帖子,包括你嘗試過的新代碼。 – 2014-11-09 15:44:03

+0

我明白了。我確實沒有返回和'System.out.println(arabicToRoman(number))';'它試了一下,但我的問題是之後的任何正確的輸入被添加到前一個。我用我試過的新代碼更新了我的帖子 – User121x 2014-11-09 18:13:09

相關問題