为什么在方法中声明变量不起作用

时间:2019-03-07 15:31:06

标签: java

为什么x变量的声明不起作用?它显示error : cannot find symbol我是Java编程的新手

public static void CompareMark() {
    double x;
    System.out.print("Enter mark: \n");
    x = scan.nextDouble();
    if (x >= 0 && x <= 100) {
        if (x >= 0 && x <= 49)
            System.out.println("Grade:F");
        else if (x >= 50 && x <= 59)
            System.out.println("Grade:C");
        else if (x >= 60 && x <= 74)
            System.out.println("Grade:B");
        else
            System.out.println("Grade:A");
    } else
        System.out.println("Invalid marks");
}

2 个答案:

答案 0 :(得分:0)

您尚未声明扫描:

Scanner scan = new Scanner(System.in);

答案 1 :(得分:0)

import java.util.Scanner;

public class Testing {

    public static void main(String[] args) {
        compareMark();
    }

    public static void compareMark() {
        double x;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter mark: \n");
        x = scan.nextDouble();
        if (x >= 0 && x <= 100) {
            if (x >= 0 && x <= 49)
                System.out.println("Grade:F");
            else if (x >= 50 && x <= 59)
                System.out.println("Grade:C");
            else if (x >= 60 && x <= 74)
                System.out.println("Grade:B");
            else
                System.out.println("Grade:A");
        } else
            System.out.println("Invalid marks");
    }
}