2016-10-10 13 views
4

私はJavaクラスの最後の段階でこの情報を完成させようとしています。情報が正しいかどうかをユーザーに尋ねた後にプログラムを実行すると、問題なく最初の質問に戻ります何。ここでは、コードです:利息計算機に結果が表示されない

import java.util.Scanner; 

public class InterestCalculator { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String userResponse = null; 

     do { 
      int quartersDisplayed = -1; 
      double startingBalance = -1, 
        interestRate = -1; 

      do { 
       System.out.println("Enter the numbers of quarters you wish to display that is greater than zero and less or equal to 10: "); 
       userResponse = input.next(); 

       try{ 
        quartersDisplayed = Integer.parseInt(userResponse); 
       } catch(NumberFormatException e) { 

       } 

       if(quartersDisplayed <= 0 || quartersDisplayed > 10) { 
        System.out.println("Sorry, that value is not valid."); 
       } else { 
        break; 
       } 
      } while(true); 


      do { 
       System.out.println("Enter the starting balance (must be greater than zero): "); 
       userResponse = input.next(); 

       try { 
        startingBalance = Double.parseDouble(userResponse); 
       } catch(NumberFormatException e) { 

       } 

       if(startingBalance <= 0) { 
        System.out.println("Sorry, that value is not valid."); 
       } else { 
        break; 
       } 
      } while(true); 


      do { 
       System.out.println("Enter the interest rate (greater than zero less than twenty percent): "); 
       userResponse = input.next(); 

       try { 
        interestRate = Double.parseDouble(userResponse); 
       } catch(NumberFormatException e) { 

       } 

       if(interestRate <= 0 || interestRate > 20){ 
        System.out.println("Sorry, that value is not valid."); 
       } else { 
        break; 
       } 
      } while(true); 


      System.out.println("You have entered the following amount of quarters: " 
          + quartersDisplayed); 
      System.out.println("You also entered the starting balance of: " + startingBalance); 
      System.out.println("Finally, you entered the following of interest rate: " 
          + interestRate); 
      System.out.println("If this information is not correct, please exit the program and enter the correct information."); 

      double quarterlyEndingBalance = startingBalance + (startingBalance * interestRate/100 * .25); 
      System.out.println("Your ending balance for your quarters is " 
        + quarterlyEndingBalance); 
      System.out.println("Do you want to continue?"); 
      userResponse = input.next(); 

      if("y".equalsIgnoreCase(userResponse) || "yes".equalsIgnoreCase(userResponse)) 
       continue; 
      else 
       break; 

     } while(true); 
    } 
} 

私は何を探していますサンプル出力として:

Enter number of quarters from 1 to 10 
5 
Enter the beginning principal balance greater than zero 
4500 
Enter the interest rate percentage without the percent sign, greater than 0 percent and less than/equal to 20% 
3.5 
You entered a principal balance of $4500.0 for 5 quarters at 3.5% interest. 
Is this correct? (y/n) 
y 
Quarter  Beginning  Interest  Ending 
Number  Balance   Earned   Balance 
1    4500.00   39.38   4539.38 

電気ショック療法電気ショック療法

+1

あなたは 'continue'文が何をしているのか混乱していると思います。これは、 "ループの後にコードを続ける"ではなく、 "ループを再実行する"という意味です。ループを終了し、ループの後にコードを続行したい場合は、 'break'を使います。基本的にあなたは 'continue'と' break'を逆にしていますが、 'continue'ステートメントはとにかく無益です。 – ajb

答えて

0

入力された場合はたぶん、あなたがするメンター何、ループを再起動することです違う。この場合、continuebreakを切り替えるだけです。

continueステートメントは、ループをすぐに次の反復を開始させます。空の文;でそれを削除するか、またはifの条件を無効にしてelseを削除し、breaktrueにしてください。さらに、あなたのコードは何もしないで、入力を取得し、それが正しいかどうかを尋ねます。 do...while(true)ループ内に書かれているので、このループは決して終了しません。したがって、そのループを削除するか、プロセスを中止するオプションを追加してください。

+0

私は元の投稿を編集したことを理解していると思います。このサンプルを実行するにはどうすればよいですか?すみません、私はとても無知です。 – jh365

+0

@ jh365第1オフ:あなたはコードを教えて結果を印刷しません。あなたはそれを自分で作ることができますか、それとも助けが必要ですか? – Syntac

+0

ahhhhhもしあなたが伝説になるのを手伝ってもらえればいいけど、これがなければ私は自分自身でやる必要がある多くの仕事です。ただ無名です – jh365

0
Your code should look something like the one below... 
**Look out to the bottom, where the user is informed of the values entered and check for the changes made**. This is quite simple... 


import java.util.Scanner; 

public class correctstack{ 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String userResponse = null; 

     do { 
      int quartersDisplayed = -1; 
      double startingBalance = -1, 
        interestRate = -1; 

      do { 
       System.out.println("Enter the numbers of quarters you wish to display that is greater than zero and less or equal to 10: "); 
       userResponse = input.next(); 

       try{ 
        quartersDisplayed = Integer.parseInt(userResponse); 
       } catch(NumberFormatException e) { 

       } 

       if(quartersDisplayed <= 0 || quartersDisplayed > 10) { 
        System.out.println("Sorry, that value is not valid."); 
       } else { 
        break; 
       } 
      } while(true); 


      do { 
       System.out.println("Enter the starting balance (must be greater than zero): "); 
       userResponse = input.next(); 

       try { 
        startingBalance = Double.parseDouble(userResponse); 
       } catch(NumberFormatException e) { 

       } 

       if(startingBalance <= 0) { 
        System.out.println("Sorry, that value is not valid."); 
       } else { 
        break; 
       } 
      } while(true); 


      do { 
       System.out.println("Enter the interest rate (greater than zero less than twenty percent): "); 
       userResponse = input.next(); 

       try { 
        interestRate = Double.parseDouble(userResponse); 
       } catch(NumberFormatException e) { 

       } 

       if(interestRate <= 0 || interestRate > 20){ 
        System.out.println("Sorry, that value is not valid."); 
       } else { 
        break; 
       } 
      } while(true); 


System.out.println("You have entered the following amount of quarters: "+ quartersDisplayed); 
System.out.println("You also entered the starting balance of: " + startingBalance); 
System.out.println("Finally, you entered the following of interest rate: "+ interestRate); 
System.out.println("If this information is not correct, please exit the program and enter the correct information."); 

//Here, you give the user the opportunity to continue or re-enter the values. So you go like... 

System.out.println("Is this info correct?"); 

String user_input = input.next(); 

if("y".equalsIgnoreCase(user_input) || "yes".equalsIgnoreCase(user_input)){ 

      double quarterlyEndingBalance = startingBalance + (startingBalance * interestRate/100 * .25); 
      System.out.println("Your ending balance for your quarters is "+ quarterlyEndingBalance); 


     System.out.println("\n\nDo you want to continue?"); 
       userResponse = input.next(); 

      if("y".equalsIgnoreCase(userResponse) || "yes".equalsIgnoreCase(userResponse)) 
        continue; 
     else 
      break; 
} 
else 
    continue; 


     } while(true); 
    } 
} 
関連する問題