How To Limit User Input In Java -


the amount of investment must positive , can value. period of investment in years should positive. annual rate of interest can between 0.25% 14%.

import java.util.scanner;  public class interestcalculator{      /**      * @param args command line arguments      */     public static void main(string[] args) {         // todo code application logic here          scanner input = new scanner(system.in);          // entering interest rate         system.out.print("please enter annual interest rate between 0.25 10 : ");         double annualinterestrate = input.nextdouble();          double monthlyinterestrate = annualinterestrate / 1200;          system.out.print("enter number of years: ");         int numberofyears = input.nextint();          // entering amount earned         system.out.print("enter amount: ");         double amountofinterest = input.nextdouble();          // calculating         double moneyearned = amountofinterest * monthlyinterestrate;          // displaying results         system.out.println("the money earned $" +         (int) (moneyearned * 100) / 100.0);         int i;          (i = 1; <= numberofyears * 12; i++) {             double balance = amountofinterest + moneyearned;             amountofinterest = balance;             monthlyinterestrate = moneyearned + 0.01;             system.out.println(i + "\t\t" + amountofinterest                     + "\t\t" + monthlyinterestrate + "\t\t" + balance);          }      } } 

i have made basic program but, don't know how add restrictions.

are talking :

while(input.nextdouble()<0){          system.out.println("please enter positive investment");       } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -