gpt4 book ai didi

java - 得到 0 输出而不是正确的输出

转载 作者:行者123 更新时间:2023-12-02 10:00:32 28 4
gpt4 key购买 nike

我可以输入但在输出中,我得到的结果为零。我使用两门类(class),一门是主要的,是日志,另一门是成绩计算。在成绩计算器中,我编写了方法和构造函数,在日志中,类通过构造函数调用这些方法。

import java.util.Scanner;

public class LogBook {
public static void main(String[] args) {
GradeCalculation g = new GradeCalculation();
g.Askmarks();
g.finalmark();
}
}

import java.util.Scanner;

public class GradeCalculation {
Scanner kb = new Scanner(System.in);

private double quiz, midsemester, appliedproject, finaltest, finalmarks;

GradeCalculation(double quiz, double midsemester, double appliedproject, double finaltest, double finalmarks){
}

GradeCalculation(){

}

public void Askmarks(){
System.out.print("Enter the QUIZ marks: ");
double quiz = kb.nextDouble();
System.out.print("Enter the midsemester marks: ");
double midsemester = kb.nextDouble();
System.out.print("Enter the appliedproject: ");
double appliedproject = kb.nextDouble();
System.out.print("Enter the finaltest: ");
double finaltest = kb.nextDouble();
}

public void finalmark(){
finalmarks= 0.1*(quiz)+0.2*(midsemester)+0.4*(appliedproject)+0.3*(finaltest);
System.out.print("Enter the final: " + finalmarks);
}
}

最佳答案

问题在于您在 Askmarks 中定义局部变量

double quiz = kb.nextDouble();
...
double finaltest = kb.nextDouble();

它们不会影响字段

private double quiz, midsemester ,appliedproject ,finaltest,finalmarks;

因此,在 finalmark 中,您将获得每个 double 字段的默认值,即 0.0

解决方案是设置字段而不是初始化局部变量

this.quiz = kb.nextDouble();
...
this.finaltest = kb.nextDouble();

关于java - 得到 0 输出而不是正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55666518/

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