编辑我的做,让它变得更好



我有一个关于如何让我的 do while 循环更好的问题。现在我有该程序,因此当用户输入他们是否要出售,购买或丢弃时,他们会通过后续的if语句,但是我想做什么,而不是问他们是否愿意像我现在一样再次做所述事情。我想问用户他们是否愿意再次以物易物,然后给他们原来的三个选项,而不仅仅是他们最初再次选择的一个选项。任何帮助都会很棒。

int ogresponse = JOptionPane.showConfirmDialog(null, "Would you like to barter?", "Please select", JOptionPane.YES_NO_OPTION);
System.out.println(ogresponse);
if (ogresponse == 0) {
String response = JOptionPane.showInputDialog("Would you like to buy items, sell items, or discard an item? (options: Sell, Buy, or Discard)").toLowerCase();
do {
if (response.equals("discard")) {
String name_Item = JOptionPane.showInputDialog("What would you like to discard? (options: Ketchup, Mayo, Bleach, or Lysol)").toLowerCase();
remove_item(list, name_Item);
int keep_Going = JOptionPane.showConfirmDialog(null, "Would you like to discard more?", "Please select", JOptionPane.YES_NO_OPTION);
if (keep_Going == 0) {
stock();
ogresponse = 0;
} else if (keep_Going == 1) {
ogresponse = 1;
}
}
else if (response.equals("sell")) {
String name_Item = JOptionPane.showInputDialog("What would you like to sell? (options: Ketchup, Mayo, Bleach, or Lysol)").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to sell?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
sell(list, name_Item, qty_num);
int keep_Going = JOptionPane.showConfirmDialog(null, "Would you like to sell more?", "Please select", JOptionPane.YES_NO_OPTION);
if (keep_Going == 0) {
stock();
ogresponse = 0;
} else if (keep_Going == 1) {
ogresponse = 1;
}
} else if (response.equals("buy")) {
String name_Item = JOptionPane.showInputDialog("What would you like to buy? (options: Ketchup, Mayo, Bleach, or Lysol)").toLowerCase();
String qty_Amount = JOptionPane.showInputDialog("How much of said item would you like to buy?").toLowerCase();
int qty_num = Integer.parseInt(qty_Amount);
buy(list, name_Item, qty_num);
int keep_Going = JOptionPane.showConfirmDialog(null, "Would you like to buy more?", "Please select", JOptionPane.YES_NO_OPTION);
if (keep_Going == 0) {
stock();
ogresponse = 0;
} else if (keep_Going == 1) {
ogresponse = 1;
}
}
} while (ogresponse != 1);
}

我会使用布尔值。 如果用户选择以物易物,请将布尔值设置为 true 并重复 do-while。类似的东西

do{ 
String response = JOptionPane.showInputDialog("Would you like to buy items, sell items, or discard an item? (options: Sell, Buy, or Discard)").toLowerCase(); 
//The rest of the code
System.out.println("¿Repeat?");
if(UserSaysYes){
boolean=true;
else{
boolean=false;
}
}while((ogresponse != 1)||(boolean==true)

您可以做一些事情来平滑代码。

首先,我拆分出"你想...更多?您可以通过切换使用更通用的术语(例如易货贸易)甚至列出选项来做到这一点。如果这对您没有吸引力,那么我会声明一个变量String operation = "",然后根据发生的操作将operation设置为丢弃、卖出或买入。然后,在循环的末尾,你有这条线

int keep_Going = JOptionPane.showConfirmDialog(null, "Would you like to " + operation + " more?", "Please select", JOptionPane.YES_NO_OPTION);

其次,我会从直接依赖 ogresponse 切换到do whilewhile。如果要继续使用布尔变量,可以更轻松地存储。为您节省(微不足道的)内存量,但也看起来更干净,并且更清楚地了解您正在做什么。如何使用stock()函数取决于它的工作方式。我会把它放在循环的前面,因为每当访问循环时,这隐含地意味着交易将要发生,你需要你的商品库存。也就是说,我不知道该功能的细节。如果我们接受所有这些更改,我们可以从

if(ogresponse == 0){
do{
/*your code here*/
int keep_Going = JOptionPane.showConfirmDialog(null, "Would you like to discard more?", "Please select", JOptionPane.YES_NO_OPTION);
if (keep_Going == 0) {
stock();
ogresponse = 0;
} else if (keep_Going == 1) {
ogresponse = 1;
}
/*more code here*/
int keep_Going = JOptionPane.showConfirmDialog(null, "Would you like to sell more?", "Please select", JOptionPane.YES_NO_OPTION);
if (keep_Going == 0) {
stock();
ogresponse = 0;
} else if (keep_Going == 1) {
ogresponse = 1;
}
/*yet more code here*/
int keep_Going = JOptionPane.showConfirmDialog(null, "Would you like to sell more?", "Please select", JOptionPane.YES_NO_OPTION);
if (keep_Going == 0) {
stock();
ogresponse = 0;
} else if (keep_Going == 1) {
ogresponse = 1;
}
}while(ogresponse != 1)
}

boolean continueWithDialog = (JOptionPane.showConfirmDialog(null, "Would you like to barter?", "Please select", JOptionPane.YES_NO_OPTION) == 0)
while(continueWithDialog){
stock(); //You could put this after the dialog and add a check, if(continueWithDialog) if stock() is a particularly sensitive function.
/*your code here*/
continueWithDialog = (JOptionPane.showConfirmDialog(null, "Would you like to barter more?", "Please select", JOptionPane.YES_NO_OPTION) == 1);
}

您还可以将许多冗长的if块拆分为自己的方法,从而生成更干净的主循环块。你也可以做一个switch语句,但我个人不是switch的忠实粉丝,而不是if.

while(continueWithDialog){
stock();
String response = JOptionPane.showInputDialog("Would you like to buy items, sell items, or discard an item? (options: Sell, Buy, or Discard)").toLowerCase();
if(response.equals("discard"))
barter();
else if(response.equals("sell"))
sell();
else if(response.equals("buy"))
buy();
else
/*What if they don't enter one of the expected values?*/
continueWithDialog = (JOptionPane.showConfirmDialog(null, "Would you like to barter more?", "Please select", JOptionPane.YES_NO_OPTION) == 1);
}

现在将它们分开似乎很愚蠢,但如果您需要重用它们,您会感谢您这样做!即使排除重用情况,它也提供了一些清晰度,并且看起来更整洁。

最新更新