3 回答
TA贡献1871条经验 获得超13个赞
该变量num在代码的第二部分不可见,您需要在if...else语句之外声明该变量:
public static void main(String[] args){
int num = 0;
double ₪ = 3.5748;
System.out.println("Enter the price");
...
}
TA贡献1802条经验 获得超5个赞
num不在您使用它的范围内,if(num = 1)也不是您想要的,因为单个=将 1 分配给num而不是比较它们。为了进行比较,请使用==.
将您的代码更改为如下所示:
...
int num = 0; // some reasonable sentinel value
if (dis > 99) {
System.out.println("error");
// more error handling probably needed
} else {
System.out.println("the price is" + " " + (price-dis*(price/100)));
double priceafter = (price-dis*(price/100));
System.out.println("Would you like to exchange the discounted price from $ to ₪? (1=yes/2=no)");
num = reader.nextInt();
}
if(num == 1) {
System.out.println("The price is " + ((price-dis*(price/100))*₪) + "₪");
} else {
...
添加回答
举报