Java switch语句的重构问题
以下是执行切换的方法(注意切换语句根据要求正确工作) 现在我开始进行一些重构,并将该方法放在项目的超类(可乐、芯片和饼干)中,它看起来如下所示:Java switch语句的重构问题,java,for-loop,if-statement,switch-statement,Java,For Loop,If Statement,Switch Statement,以下是执行切换的方法(注意切换语句根据要求正确工作) 现在我开始进行一些重构,并将该方法放在项目的超类(可乐、芯片和饼干)中,它看起来如下所示: public void performChecks(){ inputPrice = priceReader.nextDouble(); inputCode = codeReader.nextLine(); initializeItems(); for(int i = 0; i < items.length; i++
public void performChecks(){
inputPrice = priceReader.nextDouble();
inputCode = codeReader.nextLine();
initializeItems();
for(int i = 0; i < items.length; i++){
if(items[i].checkCode(inputCode)){
if(items[i].checkPrice(inputPrice)){
System.out.println("You ordered " + items[i].getName() + " here it is");
break;
} else {
System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
break;
}
} else if(!items[i].checkCode(inputCode)){
continue;
} else {
System.out.println("Sorry, we don't have item with such a code");
inputCode = codeReader.nextLine();
}
}
}
inputCode = codeReader.nextLine();
do{
if(items[i].checkCode(inputCode)){
if(items[i].checkPrice(inputPrice)){
System.out.println("You ordered " + items[i].getName() + " here it is");
break;
} else {
System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
break;
}
} else {
inputCode = codeReader.nextLine();
}
} while(!items[i].checkCode(inputCode));
boolean found = false;
for(Item item : items) {
if(item.checkCode(inputCode)){
found = true;
[...] // Here you found the item and you can check the price and other stuff
}
}
if(!found) {
[...] // Here you can handle the case of the incorrect code
}
因为我很确定,它只是卡在那里,什么也不返回(对于不正确的商品代码)。我不是绝对确定,但我想你可以这样做:
public void performChecks(){
inputPrice = priceReader.nextDouble();
inputCode = codeReader.nextLine();
initializeItems();
for(int i = 0; i < items.length; i++){
if(items[i].checkCode(inputCode)){
if(items[i].checkPrice(inputPrice)){
System.out.println("You ordered " + items[i].getName() + " here it is");
break;
} else {
System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
break;
}
} else if(!items[i].checkCode(inputCode)){
continue;
} else {
System.out.println("Sorry, we don't have item with such a code");
inputCode = codeReader.nextLine();
}
}
}
inputCode = codeReader.nextLine();
do{
if(items[i].checkCode(inputCode)){
if(items[i].checkPrice(inputPrice)){
System.out.println("You ordered " + items[i].getName() + " here it is");
break;
} else {
System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
break;
}
} else {
inputCode = codeReader.nextLine();
}
} while(!items[i].checkCode(inputCode));
boolean found = false;
for(Item item : items) {
if(item.checkCode(inputCode)){
found = true;
[...] // Here you found the item and you can check the price and other stuff
}
}
if(!found) {
[...] // Here you can handle the case of the incorrect code
}
您试图实现的目标可以用一个简单的“find”布尔值来解决。像这样:
public void performChecks(){
inputPrice = priceReader.nextDouble();
inputCode = codeReader.nextLine();
initializeItems();
for(int i = 0; i < items.length; i++){
if(items[i].checkCode(inputCode)){
if(items[i].checkPrice(inputPrice)){
System.out.println("You ordered " + items[i].getName() + " here it is");
break;
} else {
System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
break;
}
} else if(!items[i].checkCode(inputCode)){
continue;
} else {
System.out.println("Sorry, we don't have item with such a code");
inputCode = codeReader.nextLine();
}
}
}
inputCode = codeReader.nextLine();
do{
if(items[i].checkCode(inputCode)){
if(items[i].checkPrice(inputPrice)){
System.out.println("You ordered " + items[i].getName() + " here it is");
break;
} else {
System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
break;
}
} else {
inputCode = codeReader.nextLine();
}
} while(!items[i].checkCode(inputCode));
boolean found = false;
for(Item item : items) {
if(item.checkCode(inputCode)){
found = true;
[...] // Here you found the item and you can check the price and other stuff
}
}
if(!found) {
[...] // Here you can handle the case of the incorrect code
}
“我很确定,它只是卡在那里了”你试过单步执行代码吗?你有
if(items[I].checkCode(inputCode))
然后else如果(!items[I].checkCode(inputCode))
这两种情况都包括在内,就不可能达到最后的else
子句。史蒂夫史密斯-是的,它卡在那里了。我甚至都不知道为什么我会这样说:)伯杰——我自己也要面对面,犯这么愚蠢的错误。。。谢谢,这解决了问题:)