我正在制作一个程序,要求 5 名学生输入他们的姓名和 gpa,这将计算最低和最高的 gpa,以及平均值。即使我已经初始化了构造函数中的变量,我仍然收到“不兼容类型”错误。
System.out.println("Please enter the gpa for the second student");
gpa2 = scan.nextDouble();
System.out.println("Please enter the name for the third student");
student3 = scan.nextLine();
System.out.println("Please enter the gpa for the third student");
gpa3 = scan.nextDouble();
System.out.println("Please enter the name for the fourth student");
student4 = scan.nextLine();
System.out.println("Please enter the gpa for the fourth student");
gpa4 = scan.nextDouble();
System.out.println("Please enter the name for the last student");
student5 = scan.nextLine();
System.out.println("Please enter the gpa for the last student");
gpa5 = scan.nextDouble();
//use scanner to get the gpa
System.out.println("Student Name /" + " Student GPA");
System.out.println("----------------------------);
System.out.println(student1 +" / "+ gpa1);
System.out.println(student2 +" / "+ gpa2);
System.out.println(student3 +" / "+ gpa3);
System.out.println(student4 +" / "+ gpa4);
System.out.println(student5 +" / "+ gpa5);
/**
* Creates objScore object and passas in student parameters to class StudentScore...name and GPA scores
*/
StudentScore objScore = new StudentScore(student1, gpa1, student2, gpa2, student3, gpa3, student4, gpa4, student5, gpa5);
1 error found:
File: C:\Users\Mikey Espinal\OneDrive\Documents\StudentScoreTester.java [line: 46]
Error: incompatible types: double cannot be converted to java.lang.String
最佳答案
您应该使用String.valueOf(gpa1)
而不是gpa1
。这会将您的 double
转换为 String
。同样的事情也需要处理其他double
(gpa2
、gpa3
、...)。
关于java - 将 double 转换为字符串时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36360809/