2014-11-04 54 views
0

我正在編寫一個程序,它將從羅馬數字轉換爲十進制數字。爲什麼代碼不響應並繼續運行?

由於某種原因,它在檢查用戶輸入時不返回值。但它已經修復,

我現在面臨的是:代碼沒有迴應我的號碼(它保持在輸入後顯示一個空白屏幕)。

我該如何解決這個問題?我的代碼中有問題嗎?我只是一個初學者,所以我所學到的只是基本的東西。

public static void main(String[] args) { 
     // Fill in the body 
     Scanner in= new Scanner(System.in); 
     String user = promptUserForNumeral(in); 
     while (user.length()!=0) { 
      int numb= convertNumeralToNumber(user); 
      System.out.println("The numeral "+user+ " is the decimal number "+numb); 
      user = promptUserForNumeral(in); 
     } 
    } 
private static String promptUserForNumeral(Scanner inScanner) { 
    // Fill in the body 
    System.out.println("Enter a roman numeral (Q to quit): "); 
    String i = inScanner.nextLine(); 
    while (i.length()<=0) { 
     System.out.println("ERROR! You must enter a non-empty line!"); 
     System.out.println("Enter a roman numeral (Q to quit): "); 
     i = inScanner.nextLine(); 
    } 
    if (i.equalsIgnoreCase("q")) { 
     System.out.println("Goodbye!"); 
     System.exit(0); 
    } 
    return i; 

} 
private static int convertNumeralToNumber(String numeral) { 
    // Fill in the body 
    int numb = 0; 
    int n=0; 
    int ch=0; 
    while (n<numeral.length()) { 
     char l= numeral.charAt(n); 
     numb=convertCharacterToNumber(l); 
     if (numb<0) { 
      System.out.println("Cannot be define"); 
      n++; 
     } 
     else if (n==numeral.length()) { 
      ch+=numb; 
     } 
     else { 
      int nnumb=convertCharacterToNumber(numeral.charAt(n)); 
      if (nnumb>numb) { 
       ch+=nnumb-numb; 
       n++;      
      } 
      else { 
       ch+=numb; 
      } 
     } 
    } 
    if (ch>3999) { 
     System.out.println("Input number must be less than 3999"); 
     numb=ch; 
    } 
    return numb; 


} 


private static int convertCharacterToNumber(char numeral) { 
    // Fill in the body 
    int n=0; 
    if (numeral=='m' || numeral =='M') { 
     return 1000; 
    } 
    else if (numeral=='d' || numeral=='D') { 
     return 500; 
    } 
    else if (numeral=='c' || numeral=='C') { 
     return 100; 
    } 
    else if (numeral=='l' || numeral=='L') { 
     return 50; 
    } 
    else if (numeral=='x' || numeral=='X') { 
     return 10; 
    } 
    else if (numeral=='v' || numeral=='V') { 
     return 5; 
    } 
    else if (numeral=='i' || numeral=='I') { 
     return 1; 
    } 
    else { 
     return -1; 
    } 

}  

}

+1

什麼具體不返回一個值?你不能指望任何人在沒有暗示從哪裏開始尋找問題的情況下翻閱一段代碼。你能否進一步解釋哪些工作不正常? – 2014-11-04 04:02:22

+0

當用戶輸入羅馬數字時,它會檢查它是否合法,在第一種方法中。所以如果它合法,它將返回什麼用戶輸入 – bscouth 2014-11-04 04:23:30

回答

0
public class stringTest { 
public static void main(String[] args) { 
    // Fill in the body 
    Scanner in= new Scanner(System.in); 
    String user = promptUserForNumeral(in); 
    while (user.length()!=0) { 
     int numb= convertNumeralToNumber(user); 
     System.out.println("The numeral "+user+ " is the decimal number "+numb); 
     user = promptUserForNumeral(in); 
    } 
} 
private static String promptUserForNumeral(Scanner inScanner) { 
    // Fill in the body 
    System.out.println("Enter a roman numeral (Q to quit): "); 
    String i = inScanner.nextLine(); 
    while (i.length()>=0) { 
     if (i.length()==0) { 
      System.out.println("ERROR! You must enter a non-empty line!"); 
      System.out.println("Enter a roman numeral (Q to quit): "); 
      i = inScanner.nextLine(); 
     } 
     else if (i.equalsIgnoreCase("q")) { 
      System.out.println("Goodbye!"); 
      System.exit(0); 
     } 
     else return i; // in your program the while is never ending, so it does not return any value. 
    } 
    return ""; 
} 
private static int convertNumeralToNumber(String numeral) { 
    // Fill in the body 
    int preNumber = 0; 
    int curNumber = 0; 
    int n=0; 
    int ch=0; 
    while (n<numeral.length()) { 
     char l= numeral.charAt(n); 
     curNumber=convertCharacterToNumber(l); 
     if (curNumber<0) { 
      System.out.println("Cannot be define"); 
      System.exit(0); 
     } 
     else { 
      // I have changed the logic to evaluated decimal Number equivalent to Roman Literal 
      if(preNumber < curNumber && n != 0) ch = curNumber - ch; 
      else ch += curNumber; 
      preNumber = curNumber; 
     } 
     n++; 
    } 
    return ch; 
} 


private static int convertCharacterToNumber(char numeral) { 
    // Fill in the body 
    if (numeral=='m' || numeral =='M') { 
     return 1000; 
    } 
    else if (numeral=='d' || numeral=='D') { 
     return 500; 
    } 
    else if (numeral=='c' || numeral=='C') { 
     return 100; 
    } 
    else if (numeral=='l' || numeral=='L') { 
     return 50; 
    } 
    else if (numeral=='x' || numeral=='X') { 
     return 10; 
    } 
    else if (numeral=='v' || numeral=='V') { 
     return 5; 
    } 
    else if (numeral=='i' || numeral=='I') { 
     return 1; 
    } 
    else { 
     return -1; 
    } 

}  
} 

你或許可以看看promptUserForNumeral方法,我認爲這是沒有必要的。你可以在main while循環中包含它來查找用戶錯誤。

+0

非常感謝,現在我非常清楚! – bscouth 2014-11-04 05:26:58

0

入住這

while (i.length()>=0) { 
    if (i.length()==0) { 
     System.out.println("ERROR! You must enter a non-empty line!"); 
     System.out.println("Enter a roman numeral (Q to quit): "); 
     i = inScanner.nextLine(); 
    } 
    else if (i.equalsIgnoreCase("q")) { 
     System.out.println("Goodbye!"); 
     System.exit(0); 
    } 
} 
return i; 

這不會退出或返回任何東西,而i.length()> 0,即回報是死代碼,如果用戶沒有輸入q 。 解決方案:指定一個帶斷點的else;那麼它會工作。

else 
    break; 
0

我會重寫你的while循環:

while (i.length()<=0) { 
    System.out.println("ERROR! You must enter a non-empty line!"); 
    System.out.println("Enter a roman numeral (Q to quit): "); 
    i = inScanner.nextLine(); 
} 
if (i.equalsIgnoreCase("q")) { 
    System.out.println("Goodbye!"); 
    System.exit(0); 
} 
return i; 
+0

非常感謝你! – bscouth 2014-11-04 05:25:07

0

你有很多冗餘的條件。問題在於這個循環:

while (i.length() >= 0) { 
     if (i.length() == 0) { 
      System.out.println("ERROR! You must enter a non-empty line!"); 
      System.out.println("Enter a roman numeral (Q to quit): "); 
      i = inScanner.nextLine(); 
     } else if (i.equalsIgnoreCase("q")) { 
      System.out.println("Goodbye!"); 
      System.exit(0); 
     } 
    } 

爲我喜歡「V」的任何值。

  • 它的長度大於零,因此它進入循環。
  • 它的長度在第一個條件中再次不爲零,因此它進入elseIf
  • 由於該值不是「q」,因此else部分也不會執行。
  • 因此,它返回到循環的開始&再次檢查條件,如果長度大於零。

所以,你有一個無限循環。再次通過你的邏輯&刪除任何不必要的條件。您也可以使用break;語句來終止循環。