gpt4 book ai didi

java - 在 Java 中从数组中添加价格?

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:15 25 4
gpt4 key购买 nike

我对我的 Java 类(class)中即将进行的测试有一些复习问题,我目前正在做的一个测试要求我们创建一个包含两个数组的咖啡馆菜单,一个包含菜单项,另一个包含价格。在询问用户他们想要菜单中的哪些项目之前,我们必须打印所有价格的平均值,然后最后打印项目的总数。

我的代码:

import java.util.Scanner;

public class cafeMenu {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
double total = 0;

//Array for storing prices
double [] cafePrice = new double[5];
cafePrice[0]= 6.99;
cafePrice[1]= 5.99;
cafePrice[2]= 2.99;
cafePrice[3]= 1.50;
cafePrice[4]= 2.50;

//Menu item array
String [] cafeDrink = new String[5];
cafeDrink[0] = "Macchiato";
cafeDrink[1] = "Latte";
cafeDrink[2] = "Americano";
cafeDrink[3] = "Tea";
cafeDrink[4] = "Cappichino";

//Welcome user and gather their menu selection
System.out.println("Welcome to our cafe! Please enjoy!");
System.out.printf("The average pricing for our drinks is: %.2f \n", + cafeAvg( cafePrice));
System.out.println("Please enter a menu selection:\n"
+ "0. Macchiato -- $6.99\n"
+ "1. Latte -- $5.99\n"
+ "2. Americano -- $2.99\n"
+ "3. Tea -- $1.50\n"
+ "4. Cappichino -- $2.50");
choice = input.nextInt();

//Add up the total
for(int i = 0; i < cafePrice.length; i++ ) {
if(choice == cafePrice[i]) {
total += cafePrice[i];
}
}
System.out.println("Your total is: " + total);
}

//Method for average menu price
public static double cafeAvg(double[] array) {
double sum = 0;
double sum2 = 0;
for(int i = 0; i < array.length; i++) {
sum += array[i];
sum2 = sum /array.length;
}
return sum2;
}
}

我还没有设置 do while 循环来继续询问用户输入,因为我有点陷入将价格加在一起的困境。我想我在 for 循环中犯了一个错误,或者可能是一个逻辑错误?无论做出什么选择,这都是我不断得到的结果:

Welcome to our cafe! Please enjoy!
The average pricing for our drinks is: 3.99
Please enter a menu selection:
0. Macchiato -- $6.99
1. Latte -- $5.99
2. Americano -- $2.99
3. Tea -- $1.50
4. Cappichino -- $2.50
4
Your total is: 0.0

任何想法将不胜感激。

最佳答案

您错误地执行了以下操作,

    if(choice == cafePrice[i]) {
total += cafePrice[i];
}

choice 是一个 int,而 Cafeprice[i] 是一个 double ...而且它们代表不同的东西。我认为你实际上想做以下事情,

total += cafePrice[choice];

而不是整个 for 循环。

这段代码对我有用,

    public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int choice;
double total = 0;

//Array for storing prices
double [] cafePrice = new double[5];
cafePrice[0]= 6.99;
cafePrice[1]= 5.99;
cafePrice[2]= 2.99;
cafePrice[3]= 1.50;
cafePrice[4]= 2.50;

//Menu item array
String [] cafeDrink = new String[5];
cafeDrink[0] = "Macchiato";
cafeDrink[1] = "Latte";
cafeDrink[2] = "Americano";
cafeDrink[3] = "Tea";
cafeDrink[4] = "Cappichino";

//Welcome user and gather their menu selection
System.out.println("Welcome to our cafe! Please enjoy!");
System.out.printf("The average pricing for our drinks is: %.2f \n", + cafeAvg( cafePrice));
System.out.println("Please enter a menu selection:\n"
+ "0. Macchiato -- $6.99\n"
+ "1. Latte -- $5.99\n"
+ "2. Americano -- $2.99\n"
+ "3. Tea -- $1.50\n"
+ "4. Cappichino -- $2.50");
choice = input.nextInt();

//Add up the total
total += cafePrice[choice];
System.out.println("Your total is: " + total);
}

//Method for average menu price
public static double cafeAvg(double[] array) {
double sum = 0;
double sum2 = 0;
for(int i = 0; i < array.length; i++) {
sum += array[i];
sum2 = sum /array.length;
}
return sum2;
}

关于java - 在 Java 中从数组中添加价格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53452492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com