hpilo

Chapter3_Logic_Ex1

Dec 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*  ==================================================
  3.       Chapter 3: Logical and conditional expressions
  4.  
  5.          Ex1: Saving for Scooter
  6.     ===================================================
  7. */
  8.  
  9. public class MyProgram {
  10.     public static void main(String[] args) {
  11.        
  12.         //variables
  13.         int scooterPrice;
  14.         int salary;
  15.         int expenses;
  16.         float month;
  17.  
  18.         Scanner s=new Scanner(System.in);
  19.        
  20.         //user input
  21.         System.out.println("Enter Scooter price: ");
  22.         scooterPrice=s.nextInt();
  23.         System.out.println("Enter your salary: ");
  24.         salary=s.nextInt();
  25.         System.out.println("Enter your monthly expenses: ");
  26.         expenses=s.nextInt();
  27.        
  28.         //display msg if cant buy
  29.         if(expenses>=salary){
  30.             System.out.println("you are not able to save money and buy a scooter!");
  31.         }
  32.         //display msg if can buy now
  33.         else if(scooterPrice<salary-expenses)
  34.             System.out.println("you can buy the scooter now!");
  35.        
  36.         //dispaly msg for how long it takes to buy
  37.         else {
  38.             month = ((float) scooterPrice / (salary - expenses));
  39.             System.out.printf("%.1f months to reach your goal", month);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment