gpt4 book ai didi

java - 变量已经定义了吗?

转载 作者:行者123 更新时间:2023-12-02 00:05:47 25 4
gpt4 key购买 nike

在学习如何传递参数时,我遇到了一个问题,我无法弄清楚程序显示错误的原因。我将突出显示下面的代码。

public class pkgExone 
{
static void displayMarkRange(int[] ref,double[] mark, double size,double topValue,
double bottomValue, String message)
{
int index; //declare a variable index
System.out.println("\n\n"+ message+"\n\n"); //print parameter message
System.out.println("Reference \t Mark ");
System.out.println("Number \t Obtained\n");
// loop through the whole array and decide whether to print a students mark
for(index=0;index<=size-1;index=index+1)
{
if ((mark[index]>= bottomValue) && (mark[index]<=topValue))
{
System.out.println( ref[index]+" \t\t\t"+ mark[index]);
} // end if construct
} // end for loop
System.out.println("\n\n");
} // end of displayMarkRangeMethod

static double Calaverage(double [] mark, double size)
{
int index;
double total=0;
double calculatedaverage;
int validentries=0;

for (index=0;index<=size-1;index ++)

if ((mark[index]>=1) && (mark [index] <=100))
{
**double total + mark [index];
int validentries= validentries +1;**
}
**double calculatedaverage = total/validentires;**
return calculatedaverage;


}



public static void main(String[] args)
{
double [] ref;
double Averagemark;
double howMany=10;
int[] studentID = {900,901,902,903,904,905,906,907,908,909};
double[] examMark = {23,45,56,90,83,114,48,39,26,102};

displayMarkRange(studentID,examMark,howMany,100,1,"All Students that have valid marks");
displayMarkRange(studentID,examMark,howMany,100,70,"All Distinction students");
displayMarkRange(studentID,examMark,howMany,69,55,"All Merit students");
displayMarkRange(studentID,examMark,howMany,54,40,"All Pass students");
displayMarkRange(studentID,examMark,howMany,39,1,"All Fail students");
displayMarkRange(studentID,examMark,howMany,69,40,"All Pass or Merit students");
displayMarkRange(studentID,examMark,howMany,100,90,"Student that got between 90 and 100");
displayMarkRange(studentID,examMark,howMany,10,1,"Student that got between 10 and 1");
displayMarkRange(studentID,examMark,howMany,80,20,"Student that got between 80 and 20");
displayMarkRange(studentID,examMark,howMany,100,**Averagemark**,"Students that got more than the average");
displayMarkRange(studentID,examMark,howMany,Averagemark,1,"Students that got less than the average");
displayMarkRange(studentID,examMark,howMany,120,101,"Students whos marks are invalid");
}// end of psvm
}//end of public class

当我编译文件时:

Updating property file: H:\PIJOOP\ProjectSectionEleven\build\built-jar.properties
Compiling 1 source file to H:\PIJOOP\ProjectSectionEleven\build\classes
H:\PIJOOP\ProjectSectionEleven\src\projectsectioneleven\pkgExone.java:42: error: ';' expected
double total + mark [index];
H:\PIJOOP\ProjectSectionEleven\src\projectsectioneleven\pkgExone.java:42: error: not a statement
double total + mark [index];
2 errors
H:\PIJOOP\ProjectSectionEleven\nbproject\build-impl.xml:628: The following error occurred while executing this line:
H:\PIJOOP\ProjectSectionEleven\nbproject\build-impl.xml:246: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

最佳答案

这不是有效的代码

double total + mark [index];

也许你是故意的

total += mark [index];

对于给定范围,您只需声明一次变量。

而不是

    **double calculatedaverage = total/validentires;**
return calculatedaverage;

我只是

return total / validentires;
<小时/>

为了您的兴趣,我可能会这样编写该方法。注意:size 必须是 int 而不是 double。

static double average(double... marks) {
double total = 0;
int count= 0;

for(double mark: marks) {
if (mark < 1 || mark > 100) continue;

total += mark;
count++;
}
return total / count;
}

您可以为所有标记调用此方法

double average = average(marks);

或者一些标记

double average = average(Arrays.copy(marks, size)); // note size must be an int.

关于java - 变量已经定义了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com