num无法解析为真值

时间:2018-09-20 14:11:24

标签: java

我是java的新手,不理解为什么它不起作用,并在2条“ ifs”行上说“ num无法解析为veriable”。谢谢您的帮助

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 个答案:

答案 0 :(得分:0)

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 {
...

答案 1 :(得分:0)

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

 public static void main(String[] args){
        int num = 0;
        double ₪ = 3.5748;
        System.out.println("Enter the price");
        ...
}

答案 2 :(得分:0)

当前的实现中有很多错误:

  1. =if(num = 1)中的if(num = 2)应该改为===用于分配值,==用于检查是否相等。
  2. int num = reader.nextInt();在else块中,因此下一个if(num == 1)将不知道num变量。当前是您遇到的错误。

因此,请尝试以下操作:

import java.util.Scanner;
class Main{
  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();
    //
    int num = 0;    // <- Now `num` is accessible by your second if-check below
    if(dis > 99){
      System.out.println("error");
    }else{
      double priceafter = price-dis*(price/100);
      System.out.println("the price is" + " " + priceafter);
      System.out.println("Would you like to exchange the discounted price from $ to ₪? (1=yes/2=no)");
      num = reader.nextInt();  // <- And we still set it here
    }

    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");
    }
  }
}

Try it online.

需要注意的其他事项:

1:

System.out.println("the price is" + " " + (price-dis*(price/100)));
double priceafter = (price-dis*(price/100));

这里您要进行两次相同的计算,因此最好将其更改为:

double priceafter = price-dis*(price/100);
System.out.println("the price is" + " " + priceafter);

2:

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");
  }
}

我认为您实际上是在寻找if elseif else的情况,所以应该是这样:

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。

double price = reader.nextInt();

这不是.nextDouble()吗?当您仅接受整数输入时,为什么要以double开头。.discount输入也是如此。


但是我今天确实因为你而学到了一些新东西。从来不知道是有效的变量名。我猜它起某种特殊的字母的作用,因为通常只有字母_$是有效的变量名(和数字)也可以使用,但不能用作单个变量数字或在变量开头。)