gpt4 book ai didi

java - 编译错误代码: illegal start of expression

转载 作者:行者123 更新时间:2023-12-02 11:06:52 26 4
gpt4 key购买 nike

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

5年前关闭。




Improve this question




输出错误:
线程“main”中的异常 java.lang.RuntimeException:无法编译的源代码 - 表达式的非法开始

我正在创建一个类(class),计算 1 名学生参加 3 次测验 25%、1 名期中考试 25% 和 1 名期末考试 50%

package Grading;

import java.util.Scanner;


public class Grading {


public static void main(String[] args) {


Scanner input =new Scanner(System.in);
public static double quiz1, quiz2, quiz3, midterm, finalExam, Grades, totalGrade, bothQuizzes, PfinalExam, PmidTerm;
public static String studentname;
public static int Score;


public String getstudentname( )
{
return studentname;
}
public double getquiz1()
{
return quiz1;
}

public double getquiz2 ()
{
return quiz2;
}
public double getquiz3 ()
{
return quiz3;
}
public double midterm()
{
return midterm;
}

public double finalExam()
{
return finalExam;
}

public void setquiz1 (double quiz1)
{
this.quiz1 = quiz1;
}

public void setquiz2 (double quiz2)
{
this.quiz2 = quiz2;
}
public void setquiz3 (double quiz3)
{
this.quiz3 = quiz3;
}

public void setmidterm()
{
this.midterm = midterm;
}

public void setfinalExam()
{
this.finalExam = finalExam;
}

public void setGrades ()
{

}

public String toString(){

return this.quiz1 + " " + this.quiz2 + this.quiz3 + " " + this.midterm + " " + this.finalExam;

}



public static void readInput(){

System.out.println("Please enter the grade you got for the first quiz: ");
quiz1 = grades.nextInt();

while (quiz1 <0 || quiz1>10)
{
System.out.println("Please enter a grade between zero and ten: ");
quiz1 = grades.nextInt();
}

System.out.println("Please enter the grade you got for the second quiz: ");
quiz2 = grades.nextInt();

while (quiz2 <0 || quiz2>10)

{
System.out.println("Please enter a grade between zero and ten: ");
quiz2 = grades.nextInt();
}

System.out.println("Please enter the grade you got for the third quiz: ");
quiz3 = grades.nextInt();

while (quiz3 <0 || quiz3>10)

{
System.out.println("Please enter a grade between zero and ten: ");
quiz3 = grades.nextInt();
}

System.out.println("Please enter the grade you got on your midterm: ");
midterm = grades.nextInt();

while (midterm <0 || midterm>100)

{
System.out.println("Please enter a grade between 0 and 100: ");
midterm = grades.nextInt();
}

System.out.println("Please enter the grade you got on your final exam: ");
finalExam = grades.nextInt();

while (finalExam < 0 || finalExam > 100)

{
System.out.println("Please enter a grade between 0 and 100: ");
finalExam = grades.nextInt();
}
}

public static void output()

{

System.out.println(" your score for the first quiz was " + quiz1 );

System.out.println("your score for the second quiz was " + quiz2);

System.out.println("your score for the third quiz was " + quiz3);

System.out.println(" your score for the midterm was " + midterm );

System.out.println("your score for the final exam was " + finalExam);
bothQuizzes = ((quiz1 + quiz2 + quiz3)/100)*.25;
PmidTerm = (midterm/100) *.35;
PfinalExam = (finalExam/100) * .40;

System.out.println("Your total grade for these grades is " + totalGrade + "%");

System.out.println("Your total grade for these grades is " + totalGrade);

double letterGrade = totalGrade;
if (letterGrade >= 90)

{

System.out.println("Your grade is an A");

// grade = "A";

}

else if (letterGrade >= 80)

{

System.out.println("Your grade is a B");

}

else if (letterGrade >= 70)

{

System.out.println("Your grade is a C");

}

else if (letterGrade >= 60)

{

System.out.println("Your grade is a D");

}

else

{

System.out.println("Your grade is an F");



}

}}

最佳答案

我已经重写了你的程序。我希望这是您想要实现的目标。我不知道你想如何计算成绩,所以我已经标记了你可以用评论更改代码的地方。

由于您的代码在字段声明和函数体方面存在一些重大问题,因此我对它们进行了重新组织。你可以看看这段代码:

import java.util.Scanner;

public class Grading {
private int quiz1, quiz2, quiz3, midterm, finalExam; //quizzes exams tests what grades

public void readInput () {
Scanner in = new Scanner(System.in);
System.out.print("Please enter the grade you got for the first quiz: ");
quiz1 = readInt(in, 0, 10);

System.out.print("Please enter the grade you got for the second quiz: ");
quiz2 = readInt(in, 0, 10);

System.out.print("Please enter the grade you got for the third quiz: ");
quiz3 = readInt(in, 0, 10);

System.out.print("Please enter the grade you got on your midterm: ");
midterm = readInt(in, 0, 100);

System.out.print("Please enter the grade you got on your final exam: ");
finalExam = readInt(in, 0, 100);
}

public void output () {
System.out.println("Your score for the first quiz was " + quiz1);
System.out.println("Your score for the second quiz was " + quiz2);
System.out.println("Your score for the third quiz was " + quiz3);
System.out.println("Your score for the midterm was " + midterm);
System.out.println("Your score for the final exam was " + finalExam);

//dunno if this is the way you want to count it; change as needed
double quizzes = (quiz1 + quiz2 + quiz3) / 100 * 0.25;
double Pmidterm = midterm / 100 * 0.25;
double PfinalExam = finalExam /100 * 0.50;
double totalGrade = (quizzes + Pmidterm + PfinalExam) * 100.0;

char grade;
if (totalGrade >= 90)
grade = 'A';
else if (totalGrade >= 80)
grade = 'B';
else if (totalGrade >= 70)
grade = 'C';
else if (totalGrade >= 60)
grade = 'D';
else grade = 'F';

System.out.printf("Your grade is %c\n", grade);
}

private int readInt (Scanner in, int min, int max) {
int value = in.nextInt();
if (value < min || value > max) {
System.out.printf("Please enter a grade between %d and %d: ", min, max);
return readInt(in, min, max);
}
return value;
}

public static void main(String[] args) {
Grading grading = new Grading();
grading.readInput();
grading.output();
}
}

关于java - 编译错误代码: illegal start of expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42939718/

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