jeudi 2 mars 2017

Java program using while loop and if/else statements

I'm trying to complete an assignment question that I have been having some trouble with. I have attached the instructions that the program must follow along with some sample program behavior. I have attempted to try and solve this myself but my code seems to be lacking quite a bit.

Instructions & sample behavior

import java.util.Scanner;

public class Question3 {

public static void main(String[] args) {


             //declare scanner 
            Scanner keyboard = new Scanner (System.in);

            //initial amount
            System.out.println("Enter the initial amount: ");
            int initial = keyboard.nextInt();
            if (initial <= 100)
            {
                System.out.println("Please save money and come back later!!");
            }

            while (initial > 100 )
            {
                System.out.println("Do you want to make purchases (Y/N)? ");
                char c = keyboard.next().charAt(0);
                if (c == 'Y')
                {
                    System.out.println("Please enter the price of the item = ");
                    int price = keyboard.nextInt();
                    for (int i=0 ;initial - price >10; initial = initial-price )
                    {
                        System.out.println("A purchase is accepted");
                        initial -= price;
                        System.out.println("Please enter the price of the item = ");
                        price = keyboard.nextInt();
                    }

              if (initial-price<10)
                        {
                        System.out.println("Insufficient assets!!");
                        }
              else 
                    {
                     System.out.println("Lack of desire of Mr.Toto");
                    }

}

}

Aucun commentaire:

Enregistrer un commentaire