gpt4 book ai didi

java - 在 Java 中使用类时遇到很多麻烦

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:18 25 4
gpt4 key购买 nike

我正在学习如何使用类和方法,但我什么也学不会。我已经阅读了教科书中的章节和大量网页。我正在用 Java 编写一个程序,它将从 2 个测验、一个期中考试和一个期末考试中获取成绩,并输出所有成绩、最终加权成绩和字母成绩。我已经尝试了很多次,但无法使用类来完成。我能够编写一个无需实现类即可运行的程序,但我真的很想了解类。

这是我在没有类(class)的情况下做的:

import java.util.Scanner;

public class grade {
public static void main(String args[]) {


//quiz method
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the grade of Quiz 1: ");
double a = keyboard.nextInt();

System.out.printf("Please enter the grade of Quiz 2: ");
double b = keyboard.nextInt();

double c = (a + b);
double d = (c / 20);
double quizGrade = (d * 25);


//midterm method
System.out.printf("Please enter the grade of the Midterm: ");
double aa = keyboard.nextInt();
double bb = (aa / 100);
double midtermGrade = (bb * 25);


//final method
System.out.printf("Please Enter the grade of the Final: ");
double aaa = keyboard.nextInt();
double bbb = (aaa / 100);
double finalGrade = (bbb * 50);

double overallGrade = (quizGrade + midtermGrade + finalGrade);

System.out.printf("Score for Quiz 1 is: %f\n", a);
System.out.printf("Score for Quiz 2 is: %f\n", b);
System.out.printf("Score for Midterm is: %f\n", aa);
System.out.printf("Score for Final is: %f\n", aaa);


//figuring letter grade
if (overallGrade >= 90) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: A\n");
}
if (overallGrade >= 80 && overallGrade <= 89) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: B\n");
}
if (overallGrade >= 70 && overallGrade<= 79) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: C\n");
}
if (overallGrade >= 60 && overallGrade<= 69) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: D\n");
}
if (overallGrade < 60) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: F\n");
}
}

}

这就是我在类里面尝试的方式...我认为可以肯定地说我不知道​​自己在做什么:

import java.util.Scanner;

public class grade2 {
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the grade of Quiz 1: ");
double grade1 = keyboard.nextInt();
System.out.printf("Please enter the grade of Quiz 2: ");
double grade11 = keyboard.nextInt();
System.out.printf("Please enter the grade of the Midterm: ");
double grade2 = keyboard.nextInt();
System.out.printf("Please enter the grade of the Final: ");
double grade3 = keyboard.nextInt();

public static double quizgrade(double grade1, double grade11) {
//quiz
double a = grade1
double b = grade11

double c = (a + b);
double d = (c / 20);
double quizGrade = (d * 25);
return quizGrade;
}

public static double midtermgrade(double grade2) {
//midterm
double aa = grade2
double bb = (aa / 100);
double midtermGrade = (bb * 25);
return midtermGrade;
}

public static double finalgrade(double grade3) {
//final
double aaa = grade3
double bbb = (aaa / 100);
double finalGrade = (bbb * 50);
return finalGrade;
}

double overallGrade = (quizgrade + midtermgrade + finalgrade);

System.out.printf("Quiz 1 grade is: %f\n", grade1);
System.out.printf("Quiz 2 grade is: %f\n", grade11);
System.out.printf("Midterm grade is: %f\n", grade2);
System.out.printf("Final grade is: %f\n", grade3);


//figuring letter grade
if (overallGrade >= 90) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: A\n");
}
if (overallGrade >= 80 && overallGrade <= 89) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: B\n");
}
if (overallGrade >= 70 && overallGrade<= 79) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: C\n");
}
if (overallGrade >= 60 && overallGrade<= 69) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: D\n");
}
if (overallGrade < 60) {
System.out.printf("Your overall grade is: %f\n", overallGrade);
System.out.printf("Your letter grade is: F\n");
}
}

有什么帮助吗?这种 Material 令人困惑不已!

最佳答案

您的首要目标应该是让您的代码执行,以便您可以检查它并了解正在发生的事情,然后您可以在此之后了解 OOP 的其他方面。

在您的第一段代码中,您围绕过程代码包装了一个类,因为它位于您的静态 main 方法中,所以它将在您运行程序时执行。但是,在您的第二次尝试中,您没有在任何地方创建类(对象)的实例,那么如何执行呢?没有 main 方法,因此除非您在其他地方创建 grade2 的实例,否则您的代码将永远不会执行。

对于单个类的实现,您将在 grade2 对象中有一个静态 main 方法,它创建自己的一个实例(我建议使用大写名称的类名),如下所示:

public class Grade2 {

static void main(String[] args) {

Grade2 grade2 = new Grade2(); // Construct (instantiate) the object.

// Access methods etc. on the grade2 instance.
grade2.doStuff(args);
}

// Other methods etc.
}

因此,通过创建您的类的一个实例,您可以使用实例 (grade2) 调用方法和访问变量等。我不想让您重载访问(公共(public)、私有(private)、 protected )。但是,如果您能全神贯注于此,那么您将开始对 OOP 的其他方面有所了解。

祝你好运!

关于java - 在 Java 中使用类时遇到很多麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9322430/

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