从 Java 中的数组中添加价格?
Adding up prices from an Array in Java?
我的 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 -- .99\n"
+ "1. Latte -- .99\n"
+ "2. Americano -- .99\n"
+ "3. Tea -- .50\n"
+ "4. Cappichino -- .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 -- .99
1. Latte -- .99
2. Americano -- .99
3. Tea -- .50
4. Cappichino -- .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 -- .99\n"
+ "1. Latte -- .99\n"
+ "2. Americano -- .99\n"
+ "3. Tea -- .50\n"
+ "4. Cappichino -- .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 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 -- .99\n"
+ "1. Latte -- .99\n"
+ "2. Americano -- .99\n"
+ "3. Tea -- .50\n"
+ "4. Cappichino -- .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 -- .99
1. Latte -- .99
2. Americano -- .99
3. Tea -- .50
4. Cappichino -- .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 -- .99\n"
+ "1. Latte -- .99\n"
+ "2. Americano -- .99\n"
+ "3. Tea -- .50\n"
+ "4. Cappichino -- .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;
}