Updated question
I'm trying to complete an assignment question (Question 3 in the attached file) 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. I am having trouble getting the for loop to execute properly.. when running the program, it seems to just go through the process once instead of re-entering the loop until the user inputs "N". I would love some help with trying to get the while loop to function properly.
I also don't think this is the best possible code for the program, so please let me know if you can think of a better way to go about solving this. Preferably, we are meant to be able to do this using just if/else statements and a while loop.
`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