2016-10-21 14 views
2

私はjavaを初めて使用しています。私はこの割り当てをromanCalculatorに対して行っています。 私は現在計算部分を使っていますが、私は要求されたいくつかのルールに問題があります。特定の入力まで複数のスキャナ入力をループする

入力が正しくない場合、エラーを出力する必要があります。

これはルールの1つであり、これらは唯一可能な入力です。

<possible Roman number> <operator> <possible Roman number> 

OR

<possible Roman number> <operator> <possible Roman number> 

OR

. 

最初の2は、理解しやすい、と。プログラムを終了することです。

これは私が私の知識で何ができるか、すべてである:

Scanner in = new Scanner(System.in); 

    String firstRoman = in.next(); 
    String operator = in.next(); 
    String secondRoman = in.next(); 

それはちょうど入力の一度だけ1フォームを要求します。 私はこれをリクエストされたものに適用する方法を理解できません。何か助けていただければ幸いです。ありがとう!ここで

は一例です:

\begin{ipoutput} XX \end{ipoutput} 
\begin{ipinput} *xii 

\begin{ipinput} /vi \end{ipinput} 
\begin{ipoutput} XL 

\begin{ipoutput} MCDXLV \end{ipoutput} 
\begin{ipinput} . 

答えて

1
Scanner in = new Scanner(System.in); 
    String input = ""; 
    System.out.println("Enter your roman numbers\nEx: X + V\n:"); 
    while(!(input = in.nextLine()).equals(".")) 
    { 
     //assuming splitting the input around whitespace we can do the following 
     String[] userInput = input.split("\\s+"); 
     if(userInput.length == 3) 
     { 
      String firstRoman = userInput[0]; 
      String operator = userInput[1]; 
      String secondRoman = userInput[2]; 
      if(firstRoman.matches("[MCDXLV]+") && operator.matches("\\+|\\-") && secondRoman.matches("[MCDXLV]+")) 
      { 
       //we have some valid things to work with let's continue 
       System.out.println("Valid input - " + input); 
      } 
      else{ 
       System.out.println("Invalid input - " + input); 
      } 
      //do your thing 
     } 
     else{ 
      System.out.println("Invalid input - " + input); 
     } 
     System.out.println("Enter your roman numbers\nEx: X + V\n:"); 
    } 
+0

は助けをありがとうございましたが、私は間違っている第二の可能な選択肢を書きました。それはこのようにする必要があります – Lenotre

+0

<可能なローマ数字> – Lenotre

+0

例のように、私は前の関数から得た最後の答えで動作します。 – Lenotre

関連する問題