猿问

num 无法解析为可验证的

我是 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");

        }

    }

  }

}


FFIVE
浏览 206回答 3
3回答

慕桂英4014372

该变量num在代码的第二部分不可见,您需要在if...else语句之外声明该变量: public static void main(String[] args){        int num = 0;        double ₪ = 3.5748;        System.out.println("Enter the price");        ...}

12345678_0001

num不在您使用它的范围内,if(num = 1)也不是您想要的,因为单个=将 1 分配给num而不是比较它们。为了进行比较,请使用==.将您的代码更改为如下所示:...int num = 0; // some reasonable sentinel valueif (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 {...
随时随地看视频慕课网APP

相关分类

Java
我要回答