Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /* ==================================================
- Chapter 3: Logical and conditional expressions
- Ex1: Saving for Scooter
- ===================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- int scooterPrice;
- int salary;
- int expenses;
- float month;
- Scanner s=new Scanner(System.in);
- //user input
- System.out.println("Enter Scooter price: ");
- scooterPrice=s.nextInt();
- System.out.println("Enter your salary: ");
- salary=s.nextInt();
- System.out.println("Enter your monthly expenses: ");
- expenses=s.nextInt();
- //display msg if cant buy
- if(expenses>=salary){
- System.out.println("you are not able to save money and buy a scooter!");
- }
- //display msg if can buy now
- else if(scooterPrice<salary-expenses)
- System.out.println("you can buy the scooter now!");
- //dispaly msg for how long it takes to buy
- else {
- month = ((float) scooterPrice / (salary - expenses));
- System.out.printf("%.1f months to reach your goal", month);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment