Java 没有If的Else(需要帮助)
我一定是遗漏了什么,但我看不出这段代码中的错误在哪里 java:63:错误:表达式的非法开始Java 没有If的Else(需要帮助),java,if-statement,brackets,Java,If Statement,Brackets,我一定是遗漏了什么,但我看不出这段代码中的错误在哪里 java:63:错误:表达式的非法开始 }else ^ java:63:错误:“else”没有“if” }else public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean
}else
^
java:63:错误:“else”没有“if”
}else
public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count, double classadvarge){
for(int index = 0; index < count; index ++){
if (gpa[index] == 4.00){
awardsum += 1000;
if(need[index] == true){
awardsum += 500;
}else awardsum += 200;
}else
if (gpa[index] <= 3.70 && gpa[index] < 4.00){
if (need[index] == true){
awardsum += 500;
if (gpa[index] >= classaverage){
awardsum += 500;
}else
}else
}else
if (gpa[index] >= classaverage){
awardsum += 200;
}else
if (gpa[index] >= classaverage){
if (need[index] == true){
awardsum += 500;
}else
awardsum += 200;
}else
award[index] = awardsum;
return award;
}
}
还是先打开括号
if(){
} else {
)
我认为您对if/else构造的理解存在问题。您可能需要也可能不需要else场景,因此在这种情况下不需要使用else。因为您在代码中添加了多个这样的else,这似乎没有用。似乎你只是把它当作是强制使用else和if。此外,始终使用大括号开始和结束if或else块以避免错误 以下内容似乎没有必要:
if (gpa[index] >= classaverage){
awardsum += 500;
}else // seems not required
}else // seems not required
如何使用
您在返回奖励时也犯了错误;。您在循环中放置了。将其放置在循环结束后
这是格式化的代码,请尝试
public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count, double classadvarge)
{
for (int index = 0; index < count; index++)
{
if (gpa[index] == 4.00)
{
awardsum += 1000;
if (need[index] == true)
{
awardsum += 500;
}
else
{
awardsum += 200;
}
}
else if (gpa[index] <= 3.70 & gpa[index] < 4.00)
{
if (need[index] == true)
{
awardsum += 500;
if (gpa[index] >= classaverage)
{
awardsum += 500;
}
else //unused else
{
}
}
else //unused else
{
}
}
else if (gpa[index] >= classaverage)
{
awardsum += 200;
}
else if (gpa[index] >= classaverage)
{
if (need[index] == true)
{
awardsum += 500;
}
else
{
awardsum += 200;
}
}
else
{
award[index] = awardsum;
}
// return award; // you wrongly placed the return statement
}
return award; // This will return the whole array
}
问题通常是代码缩进不好,无法查看ifs和ELSE是否匹配。小心修理这个。但是是的,你的代码没有意义,因为你在顶部有一个悬空的else。你的else没有意义。事实上,第一个else似乎在空间中悬空,在所有方法和构造函数之外。请尽量用你的问题发布你的实际代码,除非你的目的只是为了迷惑我们。你错过了返回声明。它在第一次迭代中返回奖励。将其放置在for循环之后
public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count, double classadvarge)
{
for (int index = 0; index < count; index++)
{
if (gpa[index] == 4.00)
{
awardsum += 1000;
if (need[index] == true)
{
awardsum += 500;
}
else
{
awardsum += 200;
}
}
else if (gpa[index] <= 3.70 & gpa[index] < 4.00)
{
if (need[index] == true)
{
awardsum += 500;
if (gpa[index] >= classaverage)
{
awardsum += 500;
}
else //unused else
{
}
}
else //unused else
{
}
}
else if (gpa[index] >= classaverage)
{
awardsum += 200;
}
else if (gpa[index] >= classaverage)
{
if (need[index] == true)
{
awardsum += 500;
}
else
{
awardsum += 200;
}
}
else
{
award[index] = awardsum;
}
// return award; // you wrongly placed the return statement
}
return award; // This will return the whole array
}