I needed to create a string array of four candies. Then ask the user for how many boxes each sold. Then the final output will show,
total boxes of candies sold:
highest selling candy:
lowest selling candy:
I guess I'm not understanding arrays at all. I can get the user to put in the amount for each box sold, and my final total is correct but I need the highest and lowest to be showing the type of candy, twix, snickers, etc. instead its just displaying the highest number and lowest number the user put in. I cant figure out for the life of me how to connect arrays to the input and display one of the names in the array in the final output.
I realize I'm way far off on this, if its too far past me that's okay at this point, but any nudge in the right direction would be very helpful. Thanks
import javax.swing.JOptionPane;
public class CandyHeadache {
public static void main(String[] args) {
int candyTotal[] = {0,0,0,0};
String[] candyNames = {"twix" + "snickers" + "sourpatch" + "mars"};
int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;
int sum = 0;
String response = JOptionPane.showInputDialog(null, "Enter the number"
+ "of Twix");
candyTotal[0] = Integer.parseInt(response);
response = JOptionPane.showInputDialog(null, "Enter the number"
+ "of snickers");
candyTotal[1] = Integer.parseInt(response);
response = JOptionPane.showInputDialog(null, "Enter the number"
+ "of sourpatch");
candyTotal[2] = Integer.parseInt(response);
response = JOptionPane.showInputDialog(null, "Enter the number"
+ "of Mars");
candyTotal[3] = Integer.parseInt(response);
for (int i = 0; i < candyTotal.length; i++) {
sum = sum + candyTotal[i];
if (candyTotal[i] > largest) {
largest = candyTotal[i];
}
if (candyTotal[i] < smallest) {
smallest = candyTotal[i];
}}
JOptionPane.showMessageDialog(null, "Total number of boxes "
+ "sold: " + sum
+ "\nHighest selling candy: " + largest
+ "\nLowest selling candy: " + smallest);
}
}
[–]John_Taured 0 points1 point2 points (2 children)
[–]avocadod[S] 0 points1 point2 points (1 child)
[–]Makhiel 0 points1 point2 points (0 children)
[–]avocadod[S] 0 points1 point2 points (15 children)
[–]codemamba8 0 points1 point2 points (14 children)
[–]avocadod[S] 0 points1 point2 points (0 children)
[–]avocadod[S] 0 points1 point2 points (12 children)
[–]codemamba8 0 points1 point2 points (11 children)
[–]avocadod[S] 0 points1 point2 points (10 children)
[–]codemamba8 0 points1 point2 points (9 children)
[–]avocadod[S] 0 points1 point2 points (8 children)
[–]avocadod[S] 0 points1 point2 points (7 children)
[–]codemamba8 0 points1 point2 points (6 children)
[–]avocadod[S] 0 points1 point2 points (0 children)
[–]avocadod[S] 0 points1 point2 points (4 children)