2016-10-10 74 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 

ECT ECT

+1

我想你對'continue'語句的作用感到困惑。這意味着「再次執行循環」,而不是「在循環之後繼續執行代碼」。如果您想離開循環並在循環後繼續使用代碼,請使用「break」。基本上你有'繼續'和'斷開'顛倒過來,但是'繼續'聲明在這裏無論如何都是毫無價值的。 – ajb

回答

0

也許你換貨做的是重新啓動循環,如果輸入的是錯誤。在這種情況下,您只需切換continuebreak即可。

continue語句使循環立即開始下一次迭代。您應該刪除空的語句;,或否定if-條件,刪除else並使其 break。另外,你的代碼不會做任何事情,然後獲取輸入並詢問它是否正確。因爲它是在do...while(true)循環內寫入的,所以這個循環永遠不會結束。因此,要麼刪除該循環,要麼添加一個選項以中止該過程。

+0

我想我明白了,我編輯了原文,我該怎麼做才能得到這個樣例運行?對不起,我很無能。 – jh365

+0

@ jh365首先:您沒有任何代碼可以計算並打印結果。你可以自己做,或者你需要幫助嗎? – 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); 
    } 
}