2015-09-05 55 views
-2

我是新來的Java程序,我有一門功課在當我從一個用戶作爲字符串獲取輸入,我一直在使用一個開關的結構決定將其轉換爲一個雙重和多重其與0.11111。如何將羅馬數字字符串與Java中的double相乘?

A string input from user has to be a number in roman numeral which is I, II, III, IV, and so on.... 


//This is my program 
package practiceNum; 
import java.util.Scanner; 

public class practiceNum{ 
    class stastic void main(String[] args){ 

    String num_In_Roman; //declare a string 

    Scanner keyboard = new Scanner(System.in); //read input words 

    System.out.println("Enter a roman numerals from I to X: "); //asking user input 
    num_In_Roman = keyboard.nextLine(); 

    switch(num_In_Roman){ 

    case "I": 
    break; 

    case "II": 
    break; 

    //and so on until 10 (X) in roman numerals. 
    default: 
     System.out.println("Invalid Number!"); 
    } 

    //end of body program here 
    } 

} 



Here's what i did to calculate my math but still giving me an error. 

Double value = Double.parseDouble(num_In_Roman); //convert string into double 

case "I": 
value *= .111111; 
System.out.println(value); 
break; 


someone help please. 
+0

什麼是錯誤您收到?而且,我不認爲Double會解析你的羅馬字符串... – Sategroup

+0

線程「main」中的異常java.lang.NumberFormatException:對於輸入字符串:「I」 \t at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) \t在sun.misc.FloatingDecimal.parseDouble(來源不明) \t在java.lang.Double.parseDouble(來源不明) \t在roman_Numerals.roman_Numerals.main(roman_Numerals.java:60) –

+0

有你有你的答案。正如我所提到的羅馬字符串不帶雙 – Sategroup

回答

1

這是不錯的方法,但是這是你可以做什麼:

case "I": 
value = 1 * .111111; 
System.out.println(value); 
break; 

case "II": 
value = 2 * .111111; 
System.out.println(value); 
break; 
+0

「這是糟糕的方式」 - 完全正確的。 –