如何删除此错误?请任何人帮忙 我收到错误"运算符+未定义参数类型java.util.Scanner" 不知道为什么会出现这种情况?
包com.learn.java;
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
System.out.println("Enter marks of MATHS");
Scanner math = new Scanner(System.in);
System.out.println(math.nextFloat());
System.out.println("Enter marks of ENGLISH");
Scanner eng = new Scanner(System.in);
System.out.println(eng.nextFloat());
System.out.println("Enter marks of SOCIALSCIENCES");
Scanner socialScience = new Scanner(System.in);
System.out.println(socialScience.nextFloat());
System.out.println("Enter marks of SCIENCE");
Scanner science = new Scanner(System.in);
System.out.println(science.nextFloat());
float avgMarks = averageCalculator(math, eng, socialScience, science);
System.out.println(avgMarks);
calculatePerformanceGrades(avgMarks);
}
private static float averageCalculator(Scanner math, Scanner eng,
Scanner socialScience, Scanner science) {
float averageMarks;
averageMarks=((math + eng + socialScience + science) / 4);//getting an error here(The operator + is undefined for the argument type(s) java.util.Scanner)
return averageMarks;
}
private static void calculatePerformanceGrades(float avgMarks) {
if (avgMarks < 40) {
System.out.println("Poor");
} else if (avgMarks >= 40 && avgMarks < 59) {
System.out.println("Average");
} else if (avgMarks >= 60 && avgMarks < 79) {
System.out.println("Good");
} else if (avgMarks >= 80 && avgMarks < 89) {
System.out.println("Very Good ");
} else if (avgMarks >= 90 && avgMarks <= 100) {
System.out.println("Excellent");
}
}
}
请帮助解决问题。
答案 0 :(得分:1)
您无法使用扫描仪添加数据并在以后查找平均值。你可以只对数字执行算术运算,即整数,浮点数等。而是定义你的方法接受像import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-app',
template: `
<div>
<h2 *ngIf="names && names.length">Hello {{names[0]}}</h2>
<h2 *ngFor="let n of names">Hi {{n}}</h2>
</div>
`,
})
export class App {
constructor() {
this.names = [];
setTimeout(() => this.names = ['Angular2', 'Name2', 'name3'], 3000);
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
这样的输入数据类型(我会在这里取float
)
double
当您从标准输入(即键盘)获取输入时,您需要捕获该值并将该值发送到方法private static float averageCalculator(float math, float eng, float socialScience, float science) {
....
}
,如:
averageCalculator
答案 1 :(得分:0)
您正在使用扫描仪执行无法完成的算术运算。 而是将float参数传递给方法并进行操作。
public static void main(String[] args) {
System.out.println("Enter marks of MATHS");
Scanner math = new Scanner(System.in);
float math1=(math.nextFloat());
System.out.println("Enter marks of ENGLISH");
Scanner eng = new Scanner(System.in);
float eng1 = eng.nextFloat();
System.out.println("Enter marks of SOCIALSCIENCES");
Scanner socialScience = new Scanner(System.in);
float socialScience1= socialScience.nextFloat();
System.out.println("Enter marks of SCIENCE");
Scanner science = new Scanner(System.in);
float science1= science.nextFloat();
float avgMarks = averageCalculator(math1, eng1, socialScience1, science1);
System.out.println(avgMarks);
calculatePerformanceGrades(avgMarks);
}
private static float averageCalculator(float math, float eng,
float socialScience, float science) {
float averageMarks;
averageMarks=((math + eng + socialScience + science) / 4);
return averageMarks;
}
private static void calculatePerformanceGrades(float avgMarks) {
if (avgMarks < 40) {
System.out.println("Poor");
} else if (avgMarks >= 40 && avgMarks < 59) {
System.out.println("Average");
} else if (avgMarks >= 60 && avgMarks < 79) {
System.out.println("Good");
} else if (avgMarks >= 80 && avgMarks < 89) {
System.out.println("Very Good ");
} else if (avgMarks >= 90 && avgMarks <= 100) {
System.out.println("Excellent");
}
}