Answer to Question #249633 in Java | JSP | JSF for emily92

Question #249633

Chapter 7Assignment(Popular Candy)–15points Your goal is to record the sales for 4 different types of candy and determine the total sales and the names of the highest and lowest selling candies.

•Create a String array that stores four different types of candy. Pick whatever candy you would like.(3points)•

Have the program prompt the user to enter the number of candy boxes sold for each type of candy using an array. Do NOT accept negative values for the number of candy boxes sold. Store these numbers into an integer array.(6 points )

•Display the total sales, and the names of the highest selling and lowest selling candy items.(6points)



1
Expert's answer
2021-10-11T12:21:51-0400
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] candies = {"Snickers", "Haribo", "Mars", "Nuts"};
        int[] sales = new int[candies.length];
        int lowest = 0;
        int highest = 0;
        int total = 0;
        for (int i = 0; i < candies.length; i++) {
            System.out.println(candies[i] + " sales:");
            do {
                sales[i] = in.nextInt();
            } while (sales[i] < 0);
            if (sales[lowest] > sales[i]) {
                lowest = i;
            }
            if (sales[highest] < sales[i]) {
                highest = i;
            }
            total += sales[i];
        }
        System.out.println("Total: " + total + ", Lowest: " + candies[lowest] + " " + sales[lowest] +
                ", Highest: " + candies[highest] + " " + sales[highest]);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS