[英]Math Logic in Java for Lemonade Stand Game
我正在用Java编写柠檬水摊位游戏,但在尝试找出完美配方时遇到了一些麻烦。 我的意思是,如果天气晴朗,温度高于85,那么完美的配方将是8个柠檬,6杯糖和12块冰块。 我已经完成了大部分代码,除了数学逻辑看起来有些差。 请让我知道如何改善代码。 我包含了所有代码,因此您可以更好地了解到目前为止所有逻辑的发展情况。
public static void main(String[] args) {
///declaration of variables
int playAgain;
int days, customers = 120;
int CUPS_PITCHER = 14;
double money = 20.00, earned = 0;
double [] cupsPrice = {0.93, 1.65, 2.77}, lemonsPrice = {0.98, 2.05, 4.38}, sugarPrice = {0.74, 1.63, 3.31}, icePrice = {0.99, 2.01, 3.95};
//inventory
///string input
String cupsS = "", lemonsS = "", sugarS = "", iceS = "";
//int input values
int cups = 0, lemons = 0, sugar = 0, ice = 0;
//String for Pitcher
double priceCup;
int lemonsPitcher, sugarPitcher, iceCup;
////array for items options
String [] cupsOptions = {"25","50","100"};
String [] lemonsOptions = {"10","30","75"};
String [] sugarOptions = {"8","20","48"};
String [] iceOptions = {"100","250","500"};
int temperature;
String [] weather = {"Sunny","Hazy","Cloudy","Overcast","Rainny"};
JOptionPane.showMessageDialog(null,"Welcome to our Lemonade Stand Game");
days = getDays();
///set flag for loops
boolean ingredients, cupsFlag, lemonsFlag, sugarFlag, iceFlag;
boolean [] flag = {false,false,false,false};
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
String [] itemName = {"Cups","Lemons","Sugar","Ice Cubes"};
String [][] itemOptions = {cupsOptions,lemonsOptions,sugarOptions,iceOptions};
for(int i=1; i<=days; i++){
/////random weather
Random rand = new Random();
int randomWeather = rand.nextInt(5);
//random temperature from 59 to 99
temperature = rand.nextInt(99-59) + 59;
int response = 0;
///loop while not play game
while(response != 1){
String[] options = new String[] {"Buy", "Play Game", "Cancel"};
response = JOptionPane.showOptionDialog(null, "You currently have " + defaultFormat.format(money) + " Day " + i + "\nCups: " + cups + " *** Lemons: " + lemons + "\nSugar: " +
sugar + " *** Ice: "+ice + "\nHigh Temperature: " + temperature + "\nWeather Forecast: " + weather[randomWeather], "Inventory",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
ingredients = false;
///if buy ingredientes ** when they click buy
while(response == 0 && !ingredients){
String[] optionsBuy = new String[] {"Buy Cups", "Buy Lemons", "Buy Sugar", "Buy Ice","Back to Game"};
String line1 = "\n25 Cups: $" + cupsPrice[0] + " 10 Lemons: $" + lemonsPrice[0]
+ " 8 Cups of Sugar: $" + sugarPrice[0] + " 100 Ice Cubes: $"+icePrice[0];
String line2 = "\n50 Cups: $" + cupsPrice[1] + " 30 Lemons: $" + lemonsPrice[0]
+ " 20 Cups of Sugar: $" + sugarPrice[1] + " 250 Ice Cubes: $"+icePrice[1];
String line3 = "\n100 Cups: $" + cupsPrice[2] + " 75 Lemons: $" + lemonsPrice[2]
+ " 48 Cups of Sugar: $" + sugarPrice[2] + " 500 Ice Cubes: $"+icePrice[2];
int responsePurchase = JOptionPane.showOptionDialog(null, "You currently have " + defaultFormat.format(money) + " Day " + i + line1 + line2 + line3 + "\nHigh Temperature: " + temperature + "\nWeather Forecast: " + weather[randomWeather], "Inventory",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, optionsBuy, optionsBuy[0]);
double [][] priceOptions = {cupsPrice,lemonsPrice,sugarPrice,icePrice};
int [] qty = {0,0,0,0};
for(int j=0; j<4; j++){
if(responsePurchase == j)
qty[j] = buyItems(itemName[j],itemOptions[j],flag[j],money);
//money = money - itemPrice[j][0];
}
///deduct money
for(int k=0;k<4;k++){
for(int j=0;j<3;j++){
if(qty[k] == Integer.parseInt(itemOptions[k][j])){
money = money - priceOptions[k][j];
}
}
}
////add items purchased
cups += qty[0];
lemons += qty[1];
sugar += qty[2];
ice += qty[3];
//System.out.println(itemOptions[0][1]);
/*
if(qty[0] == 25){
money = money - cupsPrice[0];
}
if(qty[0] == 50){
money = money - cupsPrice[1];
}
if(qty[0] == 100){
money = money - cupsPrice[2];
}
// buy lemons
cupsFlag = false;
if (responsePurchase == 0) {
int qty = buyItems("Cups",cupsOptions,cupsFlag,money);
if (qty != 0) {
cups += qty;
}
}
// buy lemons
lemonsFlag = false;
if (responsePurchase == 1) {
int qty = buyItems("Lemons",lemonsOptions,lemonsFlag,money);
if (qty != 0) {
lemons += qty;
}
}
*/
///go back to game when back to game click
if(responsePurchase == 4){
ingredients = true;
}
}///end while buy ingredients
}///end while buy
JTextField fieldCup = new JTextField("0.25");
JTextField fieldLemons = new JTextField("4");
JTextField fieldSugar = new JTextField("4");
JTextField fieldIce = new JTextField("4");
Object[] fields = {"Price per Cup in Cents", fieldCup,"Lemons per Pitcher", fieldLemons,"Sugar per Pitcher", fieldSugar, "Ice per Cup", fieldIce};
int responsePitcher = JOptionPane.showConfirmDialog(null,fields,"Price",JOptionPane.OK_CANCEL_OPTION);
if(responsePitcher == JOptionPane.CANCEL_OPTION){
int stopGame = JOptionPane.showConfirmDialog(null,"Do you wish to cancel the game? All progress will be lost","",JOptionPane.YES_NO_OPTION);
if(stopGame == JOptionPane.YES_OPTION){
System.exit(0);
}
}
while(!validateDouble(fieldCup.getText()) || !validateInt(fieldLemons.getText()) || !validateInt(fieldSugar.getText()) || !validateInt(fieldIce.getText())){
if(responsePitcher == JOptionPane.CANCEL_OPTION){
int stopGame = JOptionPane.showConfirmDialog(null,"Do you wish to cancel the game? All progress will be lost","",JOptionPane.YES_NO_OPTION);
if(stopGame == JOptionPane.YES_OPTION){
System.exit(0);
}
}
JOptionPane.showMessageDialog(null,"One of the inputs is incorrect! try Again","ERROR",JOptionPane.ERROR_MESSAGE);
responsePitcher = JOptionPane.showConfirmDialog(null,fields,"Price",JOptionPane.OK_CANCEL_OPTION);
}
priceCup = Double.parseDouble(fieldCup.getText());
lemonsPitcher = Integer.parseInt(fieldLemons.getText());
sugarPitcher = Integer.parseInt(fieldSugar.getText());
iceCup = Integer.parseInt(fieldIce.getText());
for(int k=0; k<5; k++){
if(weather[randomWeather].equals(weather[k])){
////if weather is not sunny reduce possible customers
if(!weather[randomWeather].equals(weather[0])){
customers = customers - (customers * k/15);
}
}
}
//System.out.println(customers);///testing results
//System.out.println(fieldCup.getText());///testing results
System.out.println(customers);///testing results
//showBar();
///perfect recepie
if(temperature > 58){
///if sunny
if(weather[randomWeather].equals(weather[0])){
}
}
//too expensive or not right ingredients reduce possible customers
if(priceCup > 0.25){
customers = customers - (customers * 10/100);///reduce customers by 10%
}
if(lemonsPitcher > 7 || lemonsPitcher < 5){
customers = customers - (customers * 10/100);///reduce customers by 10%
}
if(sugarPitcher > 7 || sugarPitcher < 5){
customers = customers - (customers * 10/100);///reduce customers by 10%
}
if(iceCup > 10 || iceCup < 6){
customers = customers - (customers * 15/100);///reduce customers by 15%
}
///determine max cups according to inventory
int maxCupsLemons = (lemons / lemonsPitcher) * CUPS_PITCHER;
int maxCupsSugar = (sugar / sugarPitcher) * CUPS_PITCHER;
int maxCupsIce = (ice / iceCup);
int maxCupsp = cups;
int [] maxCups = {maxCupsLemons, maxCupsSugar, maxCupsIce, maxCupsp};
System.out.println(Arrays.toString(maxCups));
int minValue = maxCups[0];
for(int m=0;m<maxCups.length;m++){
if(maxCups[m] < minValue){
minValue = maxCups[m];
}
}
System.out.println(minValue);
if(minValue < customers){
customers = minValue;
}
System.out.println(customers);///testing results
////profit
earned = priceCup * customers;
money += earned;
///deduct inventory
//14 cups per pitcher
int lemonsSpent = (customers / CUPS_PITCHER) * lemonsPitcher;
int sugarSpent = (customers / CUPS_PITCHER) * sugarPitcher;
lemons = lemons - lemonsSpent;
sugar = sugar - sugarSpent;
cups = cups - customers;
JOptionPane.showMessageDialog(null,"Your profit for day " + i + " is " + defaultFormat.format(earned));
/////reset variables for next day
customers = 120;
earned = 0;
ice = 0;
}
//JOptionPane.showMessageDialog(null,days);
}
}
我认为您的问题在于定价。 最好将其保留为14杯。 然后生成每杯的价格。
首先,您应该对一个投手中有多少杯某种成分有一个标准的度量。 这样,您将有基础。
Let's say. 8 lemons + 6 cups of sugar + 12 ice cubes = 1 pitcher
乘以杯子价格。 8 * 0.98
等,然后将其添加到其他配料价格中。
但是,如果您从价格计算中删除投手,那将变得越来越好。 因为您将通过杯子出售它。
我将为您提供制定程序逻辑的技巧。 大多数时候,最明显的方法是最简单的方法。 把自己放在程序的位置,如果您是程序,您将怎么做?
在改善程序的逻辑时,请检查程序所做的每件事。 问问自己,这是我最有可能做的事情吗?
如果您的程序有效,请不要害怕冒险。 尝试其他方式,尝试其他算法,然后阅读。 然后确定其中哪些将改善您的工作。 大多数时候,尝试新事物会导致失败,但这是苦乐参半的学习方式。
编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.