I have to write a program that uses 2 arrays. The first array stores 5 flower names, and the 2nd stores the price of each flower.
The user will input what kind of flower and how many and the program will output the cost. I’m having trouble outputting the associated cost of the particular flower with the particular cost.
I want to use 5 if statements, but am having trouble with the flowers strings. Any suggestions?
Here’s my code so far:
import java.util.Scanner;
public class FlowerCounter
{
public static void main(String[] args)
{
String[] flowers = new String[5];
double[] price = {.50, .75, 1.50, .50, .80};
Scanner keyboard = new Scanner(System.in);
System.out.println("What kind of flower would you " +
"like to purchase? \nPetunia, Pansy, Rose," +
" Violet, or Carnation?");
String index = keyboard.nextLine();
System.out.println("How many " + index +"s" + " would you like?");
int count = keyboard.nextInt();
double total = count * price.length;
System.out.println("The cost for " + count + index + " is " + total);
}
}
1 Answer