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)
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]);
}
}
Comments
Leave a comment