我必须编写一个使用 2 个数组的程序。第一个数组存储 5 个花名,第二个存储每朵花的价格。
用户输入花的种类和数量,程序输出成本。我无法用特定成本输出特定花的相关成本。
我想使用 5 个 if 语句,但在使用鲜花字符串时遇到了问题。有什么建议么?
到目前为止,这是我的代码:
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);
}
}