为了账号安全,请及时绑定邮箱和手机立即绑定

num 无法解析为可验证的

num 无法解析为可验证的

FFIVE 2021-09-29 17:28:24
我是 Java 新手,不明白为什么它不起作用,并在 2 个“ifs”行上说“num 无法解析为可验证的”。感谢您的帮助package basicJava1;import java.util.Scanner;public class DiscountCalculator{  static Scanner reader = new Scanner(System.in);  public static void main(String[] args)  {    double ₪ = 3.5748;    System.out.println("Enter the price");    double price = reader.nextInt();    //    System.out.println("Enter the discount");    double dis = reader.nextInt();    //    if (dis > 99) {        System.out.println("error");    }   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)");        int num= reader.nextInt();    }     if(num = 1) {        System.out.println("The price is " + ((price-dis*(price/100))*₪) + "₪");    }   else    {        if(num = 2) {        System.out.println("The price is " + (price-dis*(price/100)) + "$");        } else {            System.out.println("error");        }    }  }}
查看完整描述

3 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

该变量num在代码的第二部分不可见,您需要在if...else语句之外声明该变量:


 public static void main(String[] args){

        int num = 0;

        double ₪ = 3.5748;

        System.out.println("Enter the price");

        ...

}


查看完整回答
反对 回复 2021-09-29
?
12345678_0001

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 {

...


查看完整回答
反对 回复 2021-09-29
  • 3 回答
  • 0 关注
  • 197 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信