为什么我的最后两个if语句有非法的表达式开始,否则没有错误?我也不能在这里发布我的代码,因为它想要格式化,我不知道如何正确地做到这一点。如何发布实际的代码视图?
我已更改它,这是已编辑的代码。谢谢你的帮助。
//Import Java scanner
import java.util.Scanner;
//This class ask a user for their item count and informs the user of the best packing method.
public class PackingOrganizer{
public static void main(String[] args){
//declare constants and variables
int CARTONS = 4, BOXES = 5;
double containerAmount;
double containerAmount2;
//Get users item count
Scanner input = new Scanner(System.in);
System.out.print("Enter number of items : (All partial items are to be rounded up. ex. 6.5 items is rounded to 7 items)");
double itemCount = input.nextDouble();
//Check to see if input is an integer value
if (itemCount != (int) itemCount)
System.out.println("Invalid input round all partial numbers up");
//processing phase
else if (itemCount % CARTONS == 0){
containerAmount =( itemCount /CARTONS );
System.out.println("Cartons can be used. The " + itemCount + " items will require " + containerAmount + " cartons ");}
else if (itemCount % BOXES ==0){
containerAmount = (itemCount / BOXES);
System.out.println("Boxes can be used. The " + itemCount + " items will require " + containerAmount + " Boxes ");}
else if ((itemCount % BOXES != 0) && (itemCount % CARTONS != 0))
System.out.println("Neither boxes nor cartons can be used for your: " + itemCount + " items.");
else if ((itemCount % BOXES == 0) && (itemCount % CARTONS == 0 ));{
containerAmount = (itemCount/BOXES);
containerAmount2 = (itemCount/CARTONS);
System.out.println("Cartons can be used. The " + itemCount + " items will require " + containerAmount2 +"." + " Boxes can be used. The " + itemCount + " items will require" + containerAmount +" boxes ");}
}
}
答案 0 :(得分:1)
在第三个其他声明中,您缺少一些括号。整个布尔条件必须括在括号中。此外,您应该使用==而不是=,因为==检查相等性,=是赋值运算符。此外,第三个else if语句在其末尾有一个分号而不是一个左大括号,而紧跟它的语句需要在末尾添加引号,括号和分号。
答案 1 :(得分:0)
您在上一个==
而非else if
=
=
是赋值运算符,而==
检查相等(使用原语)。
答案 2 :(得分:0)
else if (itemCount % BOXES != 0) && (itemCount % CARTONS != 0);
System.out.println("Neither boxes nor cartons can be used for your: " + itemCount + " items.
else if (itemCount % BOXES = 0) && (itemCount % CARTONS = 0 );{
应该这样,在else之后没有分号if if statments。你说当你把它们放在那里时你的语句就完成了,编译器会被下面的其他语句弄糊涂。
编辑:也正如其他答案所述,将你的ifs包装在一起,你应该在进行比较时使用==。
else if ((itemCount % BOXES != 0) && (itemCount % CARTONS != 0))
System.out.println("Neither boxes nor cartons can be used for your: " + itemCount + " items.
else if ((itemCount % BOXES == 0) && (itemCount % CARTONS = 0 )){
答案 3 :(得分:0)
也可能包围错误以跟踪所有错误。您需要将=
修改为==
,如我所提到的另一个问题。这是重构器。您可以将其与当前代码进行比较。
import java.util.Scanner;
//This class ask a user for their item count and informs the user of the best packing method.
public class PackingOrganizer {
public static void main(String[] args) {
//declare constants and variables
int CARTONS = 4, BOXES = 5;
double containerAmount;
double containerAmount2;
//Get users item count
Scanner input = new Scanner(System.in);
System.out.print("Enter number of items : (All partial items are to be rounded up. ex. 6.5 items is rounded to 7 items)");
double itemCount = input.nextDouble();
//Check to see if input is an integer value
if (itemCount != (int) itemCount) {
System.out.println("Invalid input round all partial numbers up");
} //processing phase
else if (itemCount % CARTONS == 0) {
containerAmount = (itemCount / CARTONS);
System.out.println("Cartons can be used. The " + itemCount + " items will require " + containerAmount + " cartons ");
} else if (itemCount % BOXES == 0) {
containerAmount = (itemCount / BOXES);
System.out.println("Boxes can be used. The " + itemCount + " items will require " + containerAmount + " Boxes ");
} else if ((itemCount % BOXES != 0)
&& (itemCount % CARTONS != 0)) {
System.out.println("Neither boxes nor cartons can be used for your: " + itemCount + " items");
} else if ((itemCount % BOXES == 0)
&& (itemCount % CARTONS == 0)) {
containerAmount = (itemCount / BOXES);
containerAmount2 = (itemCount / CARTONS);
System.out.println("Cartons can be used. The " + itemCount + " items will require " + containerAmount2 + "." + " Boxes can be used. The " + itemCount + " items will require" + containerAmount + " boxes ");
}
}
}