Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java中if语句中存在的用户输入?_Java_If Statement - Fatal编程技术网

如何使用Java中if语句中存在的用户输入?

如何使用Java中if语句中存在的用户输入?,java,if-statement,Java,If Statement,所以我要做的是让用户输入他们的权重和考试1和2的分数,如果他们输入分数,使用这些变量来计算当前的分数 然而,由于分数是由用户通过if语句内部的扫描器声明的,所以它不允许我从if语句外部使用这些变量。 当我要计算csEx1和csEx2时,如何使用变量“examOneScore”和“examTwoScore” 当我尝试使用这些时,它会说“局部变量examOneScore可能尚未初始化。” 您必须在if语句之外定义稍后要使用的变量: public static void main(String[] a

所以我要做的是让用户输入他们的权重和考试1和2的分数,如果他们输入分数,使用这些变量来计算当前的分数

然而,由于分数是由用户通过if语句内部的扫描器声明的,所以它不允许我从if语句外部使用这些变量。 当我要计算csEx1和csEx2时,如何使用变量“examOneScore”和“examTwoScore”

当我尝试使用这些时,它会说“局部变量examOneScore可能尚未初始化。”


您必须在if语句之外定义稍后要使用的变量:

public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    System.out.printf("Weight of Exam 1: ");
    double weightExamOne = keyboard.nextDouble();

    System.out.printf("Weight of Exam 2: ");
    double weightExamTwo = keyboard.nextDouble();

    System.out.printf("Do you know your score of first exam? ");
    String examOne = keyboard.nextLine();

    double examOneScore = 1;
    if(examOne.equalsIgnoreCase("yes") || examOne.equalsIgnoreCase("y"))
    {
        System.out.printf("Your score? ");
        examOneScore = keyboard.nextDouble();
    }

    System.out.printf("Do you know your score of second exam? ");
    String examTwo = keyboard.nextLine();

    double examTwoScore = 1;
    if(examTwo.equalsIgnoreCase("yes") || examTwo.equalsIgnoreCase("y"))
    {
        System.out.printf("Your score? ");
        examTwoScore = keyboard.nextDouble();
    }

    double csEx1 = (weightExamOne * examOneScore);
    double csEx2 = (weightExamTwo * examTwoScore );
}

我使用值1来定义它们,您必须查找您自己想要在那里使用的值

那么您面临的问题是什么?如果用户不知道他们第一次考试的分数,您希望
examOneScore
是什么?如果答案是“我不在乎”,那么
csEx1
呢?
public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    System.out.printf("Weight of Exam 1: ");
    double weightExamOne = keyboard.nextDouble();

    System.out.printf("Weight of Exam 2: ");
    double weightExamTwo = keyboard.nextDouble();

    System.out.printf("Do you know your score of first exam? ");
    String examOne = keyboard.nextLine();

    double examOneScore = 1;
    if(examOne.equalsIgnoreCase("yes") || examOne.equalsIgnoreCase("y"))
    {
        System.out.printf("Your score? ");
        examOneScore = keyboard.nextDouble();
    }

    System.out.printf("Do you know your score of second exam? ");
    String examTwo = keyboard.nextLine();

    double examTwoScore = 1;
    if(examTwo.equalsIgnoreCase("yes") || examTwo.equalsIgnoreCase("y"))
    {
        System.out.printf("Your score? ");
        examTwoScore = keyboard.nextDouble();
    }

    double csEx1 = (weightExamOne * examOneScore);
    double csEx2 = (weightExamTwo * examTwoScore );
}