java - 将 double 转换为字符串时遇到问题

标签 java string constructor double syntax-error

我正在制作一个程序,要求 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(gpa2gpa3、...)。

关于java - 将 double 转换为字符串时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36360809/

相关文章:

Java Tic Tac Toe 错误

c - 如何识别用户输入的字符串中的所有元音

c# - 寻求处理具有重叠字段的结构构造函数的最佳方法

javascript 原型(prototype)和内存

如果使用 4 参数版本但不使用 2 参数版本,java.net.Socket 构造函数会抛出 Socket 异常

java - 带有 SurfaceView 和逻辑单元的 Android 游戏

java - 格式化日志文件中的 cpu 值

java - 如何使用 Java 套接字将加密添加到我自己的基于 TCP 的协议(protocol)中?

java - 箭头运算符 '->' 在 Java 中起什么作用?

.net - 这是在 vb.net 中用 null 初始化字符串的最佳方法