我不知道我做了什么,但是我在运行主程序时没有通过switch语句。它昨晚工作,我还没有改变它,我只是添加了一个打印语句用于调试目的。这是我为课堂作业而必须做的POS系统的一部分。
package MultiBuy;
import static POS.POS_System.NonGSTVodka;
import static POS.POS_System.PRICE_FORMAT;
import static POS.POS_System.Vodka;
import static POS.POS_System.btnCancelPrevious;
import static POS.POS_System.nPreviousPrice;
import static POS.POS_System.nTotal;
import static POS.POS_System.strPreviousDrink;
import static POS.POS_System.txtBill;
import static POS.POS_System.spaces;
public class MultiBuy {
public static int clicked;
public static double DiscountAmt = 0.05;
public static double Discount_PRICE_Vodka = 0.385;
public static double NewVodkaPrice;
public static void MultiBuy(){
POS.POS_System.btnVodkaPressed = true;
System.out.println("No Cases Ran");
switch(clicked){
case 1:
if(clicked == 0){
//Plus 1 to clicked to create event
clicked++;
System.out.println("Case 1 Completed");
break;
}
case 2 :
if(clicked >= 2 && POS.POS_System.btnVodkaPressed == true){
txtBill.setText(txtBill.getText() + "\n" +
" " + strPreviousDrink +
spaces(40 - strPreviousDrink.length()) + "-" +
PRICE_FORMAT.format(nPreviousPrice) + "\n" + " (Canceled)\n");
nTotal -= nPreviousPrice + NewVodkaPrice;
btnCancelPrevious.setEnabled(false);
NewVodkaPrice = Vodka - Discount_PRICE_Vodka;
POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "\n" + "Multibuy Special = " + MultiBuy.Discount_PRICE_Vodka);
POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "New Price = " + NewVodkaPrice);
POS.POS_System.nTotal = POS.POS_System.nTotal;
clicked--;
System.out.println("Case 2 Completed");//Should equal 3
break;
}
case 3 :
if(clicked <= 1){
clicked++;
System.out.println("Case 3 Completed");//Should equal 2
break;
}
}
}
}
答案 0 :(得分:2)
首先有几个问题。值得注意的是,您从未初始化clicked的属性(或私有实例变量),也许您在其他地方执行此操作?这个可能需要在构造函数中完成(构造函数的工作是初始化私有实例变量)。从我所看到的,整数(基本类型)将为0.无论如何,我也不相信你正确地使用了switch语句。切换只不过是if语句的语法糖果。
例如,以下代码块是相同的
如果声明:
public int foo = 0;
if(foo == 1) {
// do things
} else if (foo == 2) {
// do things
} else {
// do things
}
切换:
public int foo = 0;
switch(foo) {
case 1:
// do things
break;
case 2:
// do things
break;
default:
// do things
break;
}
break
关键字在java中至关重要的原因很好地解释了 here ,如果您想了解这一点。
请注意,在java中,您只能打开一个int
的变量。其他语言允许您打开所有基本类型甚至自定义对象!继续......
由于这些原因,你编写的switch语句永远不会被处理,因为那些如果块是无关紧要的,并且永远不会执行你设置的方式。据我所知,这是你的编辑的代码块,我认为你想要发生什么(虽然我不能真正告诉你想做什么)。鉴于我上面解释的内容,我相信你可以更轻松地解决这个问题。
// your imports
public class MultiBuy {
public static int clicked;
public static double DiscountAmt = 0.05;
public static double Discount_PRICE_Vodka = 0.385;
public static double NewVodkaPrice;
public static void MultiBuy(){
POS.POS_System.btnVodkaPressed = true;
System.out.println("No Cases Ran");
// initialize clicked!!!!
clicked = 3;
switch(clicked){
case 0:
clicked++;
System.out.println("Case 1 Completed");
break;
default:
txtBill.setText(txtBill.getText() + "\n" +
" " + strPreviousDrink +
spaces(40 - strPreviousDrink.length()) + "-" +
PRICE_FORMAT.format(nPreviousPrice) + "\n" + " (Canceled)\n");
nTotal -= nPreviousPrice + NewVodkaPrice;
btnCancelPrevious.setEnabled(false);
NewVodkaPrice = Vodka - Discount_PRICE_Vodka;
POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "\n" + "Multibuy Special = " + MultiBuy.Discount_PRICE_Vodka);
POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "New Price = " + NewVodkaPrice);
POS.POS_System.nTotal = POS.POS_System.nTotal;
clicked--;
System.out.println("Case 2 Completed");//Should equal 3
break;
}
}
}
希望有所帮助:)