Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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数列平均值 public int CalculateResult(){ int requiredGrade=0; 对于(int x=0;x_Java_Scope - Fatal编程技术网

Java数列平均值 public int CalculateResult(){ int requiredGrade=0; 对于(int x=0;x

Java数列平均值 public int CalculateResult(){ int requiredGrade=0; 对于(int x=0;x,java,scope,Java,Scope,我正试图编写一个程序,允许用户输入一系列学生的成绩,但我不断地在return totalMarks/5行中出错(以获得5个结果的平均值) 我已经尝试将return语句移动到for循环中,但是编译器仍然无法识别什么是totalMarks。这是因为totalMarks的范围在您定义它的for循环中。要在部门中使用它,您需要在外部定义它: public int CalculateResult(){ int requiredGrade = 0; for (int x = 0; x <

我正试图编写一个程序,允许用户输入一系列学生的成绩,但我不断地在
return totalMarks/5
行中出错(以获得5个结果的平均值)


我已经尝试将return语句移动到for循环中,但是编译器仍然无法识别什么是
totalMarks

这是因为totalMarks的范围在您定义它的for循环中。要在部门中使用它,您需要在外部定义它:

public int CalculateResult(){
    int requiredGrade = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        int totalMarks = totalMarks + grade;
    }
    return totalMarks / 5;
    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
}
public int CalculateResult(){
int totalMarks=0;
int requiredGrade=0;
对于(int x=0;x<4;x++){
int grade=input.nextInt();
总分=总分+等级;
}
if(总分<要求等级){
System.out.println(“你没有通过。”);
} 
否则{
System.out.println(“你通过了”);
}
返回总计标记/5;
}

编辑-您还应按照@kurochenko Edit将返回移动到末尾-否则无法访问您的输出。

这是因为totalMarks的范围在您定义它的for循环内。要在部门中使用它,您需要在外部定义它:

public int CalculateResult(){
    int requiredGrade = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        int totalMarks = totalMarks + grade;
    }
    return totalMarks / 5;
    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
}
public int CalculateResult(){
int totalMarks=0;
int requiredGrade=0;
对于(int x=0;x<4;x++){
int grade=input.nextInt();
总分=总分+等级;
}
if(总分<要求等级){
System.out.println(“你没有通过。”);
} 
否则{
System.out.println(“你通过了”);
}
返回总分/5;
}

编辑-您还应按照@kurochenko Edit将返回移动到末尾-否则无法访问您的输出。

您需要在for循环之外声明
totalMarks
,以便其范围不限于此

另外,
return
必须是函数的最后一条指令:

public int CalculateResult(){
    int totalMarks = 0;
    int requiredGrade = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        totalMarks = totalMarks + grade;
    }

    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
    return totalMarks / 5;
}
public双重计算结果(){
int requiredGrade=10;//某些值
int totalMarks=0;
对于(int x=0;x<4;x++){
int grade=input.nextInt();
整数总分+=成绩;
}
双平均等级=总分/5;
if(平均等级<要求等级){
System.out.println(“你没有通过。”);
} 
否则{
System.out.println(“你通过了”);
}
返回平均等级;
}

您需要在for循环之外声明
totalMarks
,以便其范围不限于此

另外,
return
必须是函数的最后一条指令:

public int CalculateResult(){
    int totalMarks = 0;
    int requiredGrade = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        totalMarks = totalMarks + grade;
    }

    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
    return totalMarks / 5;
}
public双重计算结果(){
int requiredGrade=10;//某些值
int totalMarks=0;
对于(int x=0;x<4;x++){
int grade=input.nextInt();
整数总分+=成绩;
}
双平均等级=总分/5;
if(平均等级<要求等级){
System.out.println(“你没有通过。”);
} 
否则{
System.out.println(“你通过了”);
}
返回平均等级;
}

Java具有块变量可见性。您必须在使用该变量的同一块或父块(块由
{}
大括号定义)中定义变量。因此,您的代码应该如下所示:

public double CalculateResult() {
    int requiredGrade = 10; // some value
    int totalMarks = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        int totalMarks += grade;
    }
    double averageGrade = totalMarks / 5;
    if (averageGrade < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
    return averageGrade;
}
public int CalculateResult(){
int requiredGrade=0;
int totalMarks=0;
对于(int x=0;x<4;x++){
int grade=input.nextInt();
总分=总分+等级;
}
if(总分<要求等级){
System.out.println(“你没有通过。”);
} 
否则{
System.out.println(“你通过了”);
}
返回总分/5;
}

另外,在
return
语句之后应该没有代码,因为此代码是不可访问的,并且它将生成编译错误。尝试使用一些IDE(例如Netbeans、Intellij IDEA、Eclipse),以便在键入代码时显示编译错误。

Java具有块变量可见性。您必须在使用该变量的同一块或父块(块由
{}
大括号定义)中定义变量。因此,您的代码应该如下所示:

public double CalculateResult() {
    int requiredGrade = 10; // some value
    int totalMarks = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        int totalMarks += grade;
    }
    double averageGrade = totalMarks / 5;
    if (averageGrade < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
    return averageGrade;
}
public int CalculateResult(){
int requiredGrade=0;
int totalMarks=0;
对于(int x=0;x<4;x++){
int grade=input.nextInt();
总分=总分+等级;
}
if(总分<要求等级){
System.out.println(“你没有通过。”);
} 
否则{
System.out.println(“你通过了”);
}
返回总分/5;
}

另外,在
return
语句之后应该没有代码,因为此代码是不可访问的,并且它将生成编译错误。尝试使用一些IDE(例如Netbeans、Intellij IDEA、Eclipse),这样在键入代码时它会显示编译错误。

如前所述,注意变量的焦点和返回语句。 还请注意,在整数上使用除法运算符会降低结果,例如:

public int CalculateResult(){
    int requiredGrade = 0;
    int totalMarks = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        totalMarks = totalMarks + grade;
    }

    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }

    return totalMarks / 5;
}

这将打印0.0,因为i/j等于0.5,并向下舍入为0。

如前所述,请注意变量的焦点和返回语句。 还请注意,在整数上使用除法运算符会降低结果,例如:

public int CalculateResult(){
    int requiredGrade = 0;
    int totalMarks = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        totalMarks = totalMarks + grade;
    }

    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }

    return totalMarks / 5;
}

这将打印0.0,因为i/j等于0.5,四舍五入为0。

a/循环内定义的变量只能从循环内看到。这是变量范围的一部分。b/无论如何,在返回语句之后,您不能做任何事情。您的打印无效。请注意,您可能不想查看循环内定义的a/变量,但只能从循环内看到。这是变量范围的一部分。b/无论如何,在返回语句之后,您不能做任何事情。你的指纹无效