试图找出如何创建新的变量。如果您的输入是2(2个青少年)并且它们都是订单,则总金额将仅是最后一个青少年的订单,因为每个青少年订单的变量将存储在同一个变量中。我想弄清楚如果有多个同一类型的人在这家餐厅吃饭,如何创建新的变量。
import java.util.Scanner;
public class Lab9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Variables
int totalGuest;
int selectOne;
int selectTwo;
int selectThree;
int i;
//Wing Variables
double wingAmount;
//Meal Variables
double optionTotalOne = 0;
double optionTotalTwo = 0;
double optionTotalThree = 0;
//Totals
double discountOneTotal = 0;
double discountTwoTotal = 0;
double discountThreeTotal = 0;
double discountFourTotal = 0;
double entireAmount = 0;
//Menu Array
String menu[] = new String[9];
menu[0] = "Soup";
menu[1] = "Wings";
menu[2] = "Burger";
menu[3] = "Chicken Sandwhich";
menu[4] = "Fries";
menu[5] = "Pie";
menu[6] = "Ice cream";
menu[7] = "Soft drink";
menu[8] = "Coffee";
//Price Array
double prices[] = new double[9];
prices[0] = 2.50;
prices[1] = .15;
prices[2] = 4.95;
prices[3] = 5.95;
prices[4] = 1.99;
prices[5] = 2.95;
prices[6] = 2.99;
prices[7] = 1.50;
prices[8] = 1.00;
do {
System.out.println("Please type a value for NUMBER_IN_PARTY: ");
totalGuest = input.nextInt();
//for-loop
for (i = 1; i <= totalGuest; i++) {
//Order input for i-th customer
System.out.println("\n \nPlease input order information for person " + i + "\n \n");
//Discount menu displayed
System.out.println("Is this person eligible for a discount? (enter a number 1 - 4)" +
"\n1 - if CHILD 5 years of age or younger \n2 - if TEEN between 13 and 19 years of age"
+ "\n3 - if SENIOR 65 years of age or older \n4 - if NONE of the above \nPlease type a value for DISCOUNT_TYPE: ");
int discount = input.nextInt();
//If else statement
if (discount == 1) {
//Price Array for children 5 & under
prices[0] = 0;
prices[1] = 0;
prices[2] = 0;
prices[3] = 0;
prices[4] = 0;
prices[5] = 0;
prices[6] = 0;
prices[7] = 0;
prices[8] = 0;
//Menu output
System.out.println("Menu items that may be selected: \n1 " + menu[0] + "\n2 " +
menu[1] + "\n3 " + menu[2] + "\n4 " + menu[3] + "\n5 " + menu[4] + "\n6 " + menu[5] +
"\n7 " + menu[6] + "\n8 " + menu[7] + "\n9 " + menu[8]);
//Menu Item 1 prompt
System.out.println("\n \nPlease select menu item 1 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectOne = input.nextInt();
//Switch Statement
switch(selectOne) {
case 1: optionTotalOne = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalOne = wingAmount * prices[1];
break;
case 3: optionTotalOne = prices[2];
break;
case 4: optionTotalOne = prices[3];
break;
case 5: optionTotalOne = prices[4];
break;
case 6: optionTotalOne = prices[5];
break;
case 7: optionTotalOne = prices[6];
break;
case 8: optionTotalOne = prices[7];
break;
case 9: optionTotalOne = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 2 prompt
System.out.println("Please select menu item 2 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectTwo = input.nextInt();
//Switch Statement
switch(selectTwo) {
case 1: optionTotalTwo = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalTwo = wingAmount * prices[1];
break;
case 3: optionTotalTwo = prices[2];
break;
case 4: optionTotalTwo = prices[3];
break;
case 5: optionTotalTwo = prices[4];
break;
case 6: optionTotalTwo = prices[5];
break;
case 7: optionTotalTwo = prices[6];
break;
case 8: optionTotalTwo = prices[7];
break;
case 9: optionTotalTwo = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 3 prompt
System.out.println("Please select menu item 3 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectThree = input.nextInt();
//Switch Statement
switch(selectThree) {
case 1: optionTotalThree = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalThree = wingAmount * prices[1];
break;
case 3: optionTotalThree = prices[2];
break;
case 4: optionTotalThree = prices[3];
break;
case 5: optionTotalThree = prices[4];
break;
case 6: optionTotalThree = prices[5];
break;
case 7: optionTotalThree = prices[6];
break;
case 8: optionTotalThree = prices[7];
break;
case 9: optionTotalThree = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Discount One Total
discountOneTotal = optionTotalOne + optionTotalTwo + optionTotalThree;
System.out.printf("Person " + i + " Total: $%.2f" + " (Discount Type 1)", discountOneTotal);
} else if (discount == 2) {
//Price Array Teens & Seniors
prices[0] = 2.625 * .75;
prices[1] = .1575 * .75;
prices[2] = 5.1975 * .75;
prices[3] = 6.2475 * .75;
prices[4] = 2.0895 * .75;
prices[5] = 3.0975 * .75;
prices[6] = 3.1395 * .75;
prices[7] = 1.575 *.75;
prices[8] = 1.05 * .75;
//Menu output
System.out.println("Menu items that may be selected: \n1 " + menu[0] + "\n2 " +
menu[1] + "\n3 " + menu[2] + "\n4 " + menu[3] + "\n5 " + menu[4] + "\n6 " + menu[5] +
"\n7 " + menu[6] + "\n8 " + menu[7] + "\n9 " + menu[8]);
//Menu Item 1 prompt
System.out.println("\n \nPlease select menu item 1 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectOne = input.nextInt();
//Switch Statement
switch(selectOne) {
case 1: optionTotalOne = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalOne = wingAmount * prices[1];
break;
case 3: optionTotalOne = prices[2];
break;
case 4: optionTotalOne = prices[3];
break;
case 5: optionTotalOne = prices[4];
break;
case 6: optionTotalOne = prices[5];
break;
case 7: optionTotalOne = prices[6];
break;
case 8: optionTotalOne = prices[7];
break;
case 9: optionTotalOne = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 2 prompt
System.out.println("Please select menu item 2 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectTwo = input.nextInt();
//Switch Statement
switch(selectTwo) {
case 1: optionTotalTwo = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalTwo = wingAmount * prices[1];
break;
case 3: optionTotalTwo = prices[2];
break;
case 4: optionTotalTwo = prices[3];
break;
case 5: optionTotalTwo = prices[4];
break;
case 6: optionTotalTwo = prices[5];
break;
case 7: optionTotalTwo = prices[6];
break;
case 8: optionTotalTwo = prices[7];
break;
case 9: optionTotalTwo = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 3 prompt
System.out.println("Please select menu item 3 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectThree = input.nextInt();
//Switch Statement
switch(selectThree) {
case 1: optionTotalThree = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalThree = wingAmount * prices[1];
break;
case 3: optionTotalThree = prices[2];
break;
case 4: optionTotalThree = prices[3];
break;
case 5: optionTotalThree = prices[4];
break;
case 6: optionTotalThree = prices[5];
break;
case 7: optionTotalThree = prices[6];
break;
case 8: optionTotalThree = prices[7];
break;
case 9: optionTotalThree = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Discount One Total
discountTwoTotal = optionTotalOne + optionTotalTwo + optionTotalThree;
System.out.printf("Person " + i + " Total: $%.2f" + " (Discount Type 2)", discountTwoTotal);
}
else if (discount == 3) {
//Price Array Teens & Seniors
prices[0] = 2.625 * .75;
prices[1] = .1575 * .75;
prices[2] = 5.1975 * .75;
prices[3] = 6.2475 * .75;
prices[4] = 2.0895 * .75;
prices[5] = 3.0975 * .75;
prices[6] = 3.1395 * .75;
prices[7] = 1.575 *.75;
prices[8] = 1.05 * .75;
//Menu output
System.out.println("Menu items that may be selected: \n1 " + menu[0] + "\n2 " +
menu[1] + "\n3 " + menu[2] + "\n4 " + menu[3] + "\n5 " + menu[4] + "\n6 " + menu[5] +
"\n7 " + menu[6] + "\n8 " + menu[7] + "\n9 " + menu[8]);
//Menu Item 1 prompt
System.out.println("\n \nPlease select menu item 1 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectOne = input.nextInt();
//Switch Statement
switch(selectOne) {
case 1: optionTotalOne = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalOne = wingAmount * prices[1];
break;
case 3: optionTotalOne = prices[2];
break;
case 4: optionTotalOne = prices[3];
break;
case 5: optionTotalOne = prices[4];
break;
case 6: optionTotalOne = prices[5];
break;
case 7: optionTotalOne = prices[6];
break;
case 8: optionTotalOne = prices[7];
break;
case 9: optionTotalOne = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 2 prompt
System.out.println("Please select menu item 2 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectTwo = input.nextInt();
//Switch Statement
switch(selectTwo) {
case 1: optionTotalTwo = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalTwo = wingAmount * prices[1];
break;
case 3: optionTotalTwo = prices[2];
break;
case 4: optionTotalTwo = prices[3];
break;
case 5: optionTotalTwo = prices[4];
break;
case 6: optionTotalTwo = prices[5];
break;
case 7: optionTotalTwo = prices[6];
break;
case 8: optionTotalTwo = prices[7];
break;
case 9: optionTotalTwo = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 3 prompt
System.out.println("Please select menu item 3 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectThree = input.nextInt();
//Switch Statement
switch(selectThree) {
case 1: optionTotalThree = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalThree = wingAmount * prices[1];
break;
case 3: optionTotalThree = prices[2];
break;
case 4: optionTotalThree = prices[3];
break;
case 5: optionTotalThree = prices[4];
break;
case 6: optionTotalThree = prices[5];
break;
case 7: optionTotalThree = prices[6];
break;
case 8: optionTotalThree = prices[7];
break;
case 9: optionTotalThree = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
discountFourTotal = optionTotalOne + optionTotalTwo + optionTotalThree;
System.out.printf("Person " + i + " Total: $%.2f" + " (Discount Type 3)", discountFourTotal);
}else if (discount == 4) {
//Price Array for Adults
prices[0] += (.05 * prices[0]);
prices[1] += (.05 * prices[1]);
prices[2] += (.05 * prices[2]);
prices[3] += (.05 * prices[3]);
prices[4] += (.05 * prices[4]);
prices[5] += (.05 * prices[5]);
prices[6] += (.05 * prices[6]);
prices[7] += (.05 * prices[7]);
prices[8] += (.05 * prices[8]);
//Menu output
System.out.println("Menu items that may be selected: \n1 " + menu[0] + "\n2 " +
menu[1] + "\n3 " + menu[2] + "\n4 " + menu[3] + "\n5 " + menu[4] + "\n6 " + menu[5] +
"\n7 " + menu[6] + "\n8 " + menu[7] + "\n9 " + menu[8]);
//Menu Item 1 prompt
System.out.println("\n \nPlease select menu item 1 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectOne = input.nextInt();
//Switch Statement
switch(selectOne) {
case 1: optionTotalOne = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalOne = wingAmount * prices[1];
break;
case 3: optionTotalOne = prices[2];
break;
case 4: optionTotalOne = prices[3];
break;
case 5: optionTotalOne = prices[4];
break;
case 6: optionTotalOne = prices[5];
break;
case 7: optionTotalOne = prices[6];
break;
case 8: optionTotalOne = prices[7];
break;
case 9: optionTotalOne = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 2 prompt
System.out.println("Please select menu item 2 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectTwo = input.nextInt();
//Switch Statement
switch(selectTwo) {
case 1: optionTotalTwo = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalTwo = wingAmount * prices[1];
break;
case 3: optionTotalTwo = prices[2];
break;
case 4: optionTotalTwo = prices[3];
break;
case 5: optionTotalTwo = prices[4];
break;
case 6: optionTotalTwo = prices[5];
break;
case 7: optionTotalTwo = prices[6];
break;
case 8: optionTotalTwo = prices[7];
break;
case 9: optionTotalTwo = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
}
//Menu Item 3 prompt
System.out.println("Please select menu item 3 (enter a number 1 - 9) \nPlease type a value for SELECTED_ITEM: ");
selectThree = input.nextInt();
//Switch Statement
switch(selectThree) {
case 1: optionTotalThree = prices[0];
break;
case 2: System.out.println("Please enter number of wings to be ordered \nPlease type a value for NUMBER_OF_WINGS: ");
wingAmount = input.nextInt();
optionTotalThree = wingAmount * prices[1];
break;
case 3: optionTotalThree = prices[2];
break;
case 4: optionTotalThree = prices[3];
break;
case 5: optionTotalThree = prices[4];
break;
case 6: optionTotalThree = prices[5];
break;
case 7: optionTotalThree = prices[6];
break;
case 8: optionTotalThree = prices[7];
break;
case 9: optionTotalThree = prices[8];
break;
default: System.out.println("This is not an option on the menu.");
break;
}
//Discount One Total
discountThreeTotal = optionTotalOne + optionTotalTwo + optionTotalThree;
System.out.printf("Person " + i + " Total: $%.2f" + " (Discount Type 4)", discountThreeTotal);
}
}
}while(i <= totalGuest);
//Total Amount Printed
entireAmount = discountOneTotal + discountTwoTotal + discountFourTotal + discountThreeTotal;
System.out.println(discountOneTotal + " " + discountTwoTotal + " " + discountThreeTotal + " " + discountFourTotal);
System.out.printf("\n\nGrand Total for Order: $%.2f", entireAmount);
//Closed Scanner Object
input.close();
}
}
答案 0 :(得分:0)
鉴于您当前的代码,最简单的选择是将您的总变量转换为数组,例如:
Dim UserType As String
UserType = DLookup("[UserType]", "tbl_Worker", "[LoginID] = '" &
Me.txtUsername.Value & "'")
DoCmd.OpenForm "frm_Home"
Forms![frm_Home]![txtUserType] = UserType
If Form_frm_Home.txtUserType = "Admin" Then
Form_frm_Home.NavigationButton438.Enabled = True
Else
Form_frm_Home.NavigationButton438.Enabled = False
End If
这些目前尚未初始化,因此您必须在获得访客人数后对其进行初始化,例如:
double[] discountOneTotal;
double[] discountTwoTotal;
double[] discountThreeTotal;
double[] discountFourTotal;
现在每个guest虚拟机在数组中都有自己的总变量。在你的循环中,你可以使用它们:
discountOneTotal = new double[totalGuest];
discountTwoTotal = new double[totalGuest];
discountThreeTotal = new double[totalGuest];
discountFourTotal = new double[totalGuest];
注意:您也可以使用List,而不是数组,但我可能会超越您迄今为止所学到的内容。