Question: EASY JAVA Programming question I need to fix the program so thatthe loop runs and prints out prompts for the user. The program runs correctly

EASY JAVA Programming question

I need to fix the program so thatthe loop runs and prints out prompts for the user. The program runs correctly when the user enters "yes" or "no".

I tried inserting

if(answer.equals("no")) {

moreNumbers = false;

to the end but when I run it in jGrasp my output is empty

Code:

import java.util.Scanner;

public class Purchase {

public static void main(String[] args) {

// variables

Scanner keyboard;

boolean moreNumbers;

double price;

int number;

String answer;

// ask before iterating loop (with error)

keyboard = new Scanner(System.in);

moreNumbers = true;

while (moreNumbers); {

System.out.println("Enter price $");

price = keyboard.nextDouble();

System.out.print("Enter number purchased:");

number = keyboard.nextInt();

System.out.println(number + " items at $" + price);

System.out.println("Total cost $" + price*number);

System.out.println("Want to make another purchase?");

System.out.println("Enter yes or no");

answer = keyboard.next();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!