2016-06-15 7 views
0

私はいくつかの基本的な宿題をしています。それは正直なところ私が知っているものです。しかし、私は少しそれをジャズアップし、私が行方不明の何かが予期しないエラーを引き起こすことを決めた。 アイデアは、ユーザーが何かしたいかどうかを尋ねるwhileループを使用することです。彼らがyesと言う限り、ループは保持している操作を続けます(この場合、小数点を整数に丸めます)。しかし、私が「はい」とか本当に何かを入力するとすぐに、ループはそこで壊れて、丸め部分には続きません。while bufferedInputの後にループが壊れる

public class DecimalRounder { 

    //Main class method that starts and ends the program. It is prepared to throw an IO exception if need be. 
    public static void main(String args[])throws IOException 
    { 
     //Main initializes a new reader to take input from System.in 
     InputStreamReader rawInput = new InputStreamReader(System.in); 
     //Main then initializes a new buffer to buffer the input from System.in 
     BufferedReader bufferedInput = new BufferedReader(rawInput); 
     //Main initializes other used variable 
     double castInput = 0.0; 
     String contin = "yes"; 

     //Program then sets up loop to allow user to round numbers till content 
     while (contin == "yes") 
     { 

      //Program asks user if they'd like to round. 
      System.out.println("Would you like to round? Type yes to continue... "); 

      contin = bufferedInput.readLine(); 
      //If user says yes, rounding begins. ERROR FOUND HERE? 

      if (contin == "yes") //INPUT "yes" DOESN'T MATCH? 
      { 
       //Program then requests a decimal number 
       System.out.println("Please enter a decimal number for rounding: "); 
       String givenLine = bufferedInput.readLine(); 

       //rawInput is worked on using a try catch statement 
       try { 
        //givenLine is first parsed from String into double. 
        castInput = Double.parseDouble(givenLine); 
        //castInput is then rounded and outputted to the console 
        System.out.println("Rounded number is... " + Math.round(castInput)); 
        //Branch then ends restarting loop. 
       }catch(Exception e){ 
        //If the data entered cannot be cast into a double, an error is given 
        System.err.println("That is not a roundable number: " + e.getMessage()); 
        //And branch ends restarting loop. 
       } 
      } 
     } 
     System.out.println("Have a nice day!"); 
    } 
} 
+0

使用 'のequals()'ファンタスティック文字列 – Ramanlfc

答えて

0

==の代わりに.equalsを使用すると、JAVAの文字列を比較できます。 このお試しください:

contin.equals("yes") 
+0

を比較すると、私はそれを逃したか分かりません。私は今重複した質問であることを私は謝罪します。それは本当に私には問題が起きていた文字列の比較だった夜明けはありませんでした。 – Day64

+0

@ Day64:問題ありません。あなたは初心者なので、それを見つけるのはちょっと難しいです。しかしここでは、あなたのデバッグのスキルを使用する必要があります。まず、ループの途中で何かを試してみるべきでした。あなたはそれが印刷されていないことを見たでしょう。それでは、ループ中にcontinue == "yes"を出力しようとしていたはずです。あなたは偽を見たでしょう。そして、あなたは文字列を比較する上でいくつかの問題があるとわかっていただろうし、その後、Googleの検索はあなたを助けてくれたでしょう:) –

関連する問題