How do we use a variable from one method to another for eg im trying divide one variable by another

huangapple go评论148阅读模式
英文:

How do we use a variable from one method to another for eg im trying divide one variable by another

问题

我正试图在pointsaverage方法中从pointsearnedcreditsearned方法中分别获取变量,但当我运行时它显示“您的平均绩点为isNaN”,我该如何修复这个问题?(我是初学者)

public class JavaApplication40 {
    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        double credits = 0;
        double Points = 0;
        double average = 0;

        IDnumber();
        credits = CreditsEarned(credits);
        Points = PointsEarned(Points);
        System.out.println("Your grade point average is " + PointAverag(average, Points, credits));
    }

    public static void IDnumber() {
        String docNuMBER;
        System.out.println("Enter your student ID number ");
        docNuMBER = keyboard.nextLine();
    }

    public static double CreditsEarned(double credits) {
        double NumCreditsEarned;
        System.out.println("Enter your Credit hours earned");
        NumCreditsEarned = keyboard.nextDouble();
        return NumCreditsEarned;
    }

    public static double PointsEarned(double points) {
        double NumberOpoints;
        System.out.println("Enter your credit points");
        NumberOpoints = keyboard.nextDouble();
        return NumberOpoints;
    }

    public static double PointAverag(double grade, double NumberOpoints,
                                    double NumCreditsEarned) {
        double average;
        if (NumCreditsEarned != 0) {
            average = NumberOpoints / NumCreditsEarned;
        } else {
            average = 0; // Avoid division by zero
        }
        return average;
    }
}
英文:

I'm trying to divide variables from pointsearned and creditsearned method in the pointsaverage method but it gives "Your grade point average isNaN" when i run it , how do i fix this ?(I'm a beginner)

public class JavaApplication40 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
double credits = 0;
double Points = 0;
double average = 0;
IDnumber();
CreditsEarned(credits);
PointsEarned(Points);
System.out.println("Your grade point average is" +  PointAverag(average, Points, credits));
}
public static void IDnumber(){
String docNuMBER; 
System.out.println("Enter your student ID number ");
docNuMBER = keyboard.nextLine();
}
public static double CreditsEarned( double credits){
double NumCreditsEarned;
System.out.println("Enter your Credit hours earned");
NumCreditsEarned = keyboard.nextDouble(); 
return NumCreditsEarned;
}
public static double PointsEarned(double points){
double NumberOpoints;
System.out.println("Enter your credit points");
NumberOpoints = keyboard.nextDouble();
return NumberOpoints;
}
public static double PointAverag(double grade , double NumberOpoints ,
double NumCreditsEarned) {
double average ;
average =   NumberOpoints/NumberOpoints;
return average ;

答案1

得分: 2

以下是翻译好的内容:

这是您的程序中正在发生的情况:

您将 'credits' 和 'points' 设置为 0。
当您将这两个变量传递给 'pointAverag' 时,这两个变量都没有被修改。

System.out.println("Your grade point average is" + PointAverag(average, Points, credits));

这一行,字面上执行的是:

System.out.println("Your grade point average is" + PointAverag(0, 0, 0));

最终导致:

average = 0/0
<strong>'PointAverag'这会得到一个存储在 double 中的 NAN</strong>

请注意这一行:

average = NumberOpoints/NumberOpoints;

其中存在逻辑错误。它将始终存储 1 或 NAN。

如注释中所述,您需要通过将 'credits' 和 'points' 的返回值存储在您的方法 'PointsEarned' 和 'CreditsEarned' 中,来更新这些变量:

CreditsEarned(credits);
PointsEarned(Points);

变成:

credits = CreditsEarned(credits);
points = PointsEarned(Points);

希望这对您有所帮助。

英文:

Here's what's happening in your program:

You set 'credits' and 'points' to 0.
These two variables have not been modified when you get to pass them to 'pointAverag'

System.out.println(&quot;Your grade point average is&quot; +  PointAverag(average, Points, credits));

this line, literally, does this:

System.out.println(&quot;Your grade point average is&quot; +  PointAverag(0, 0, 0));

And eventually leads to:

average = 0/0

<strong>in 'PointAverag' which is NAN when stored in a double.</strong>

Watch out for this line:

average =   NumberOpoints/NumberOpoints;

it has logical mistake in it. It will always store either 1 or NAN.

As mentionned in the comments, you need to update those variables, 'credits' and 'points', by storing the returned value of your methods 'PointsEarned' and 'CreditsEarned' in them:

CreditsEarned(credits);
PointsEarned(Points);

Becomes

credits = CreditsEarned(credits);
points = PointsEarned(Points);

Hope this helps.

答案2

得分: 1

用以下代码替代:

    System.out.println("你的平均绩点是" +  PointAverag(IDnumber(), PointsEarned(Points), CreditsEarned(credits)));

如果你不使用返回值,它将被丢弃。
英文:

Write:

System.out.println(&quot;Your grade point average is&quot; +  PointAverag(IDnumber(), PointsEarned(Points), CreditsEarned(credits)));

instead.

If you don't use a returned value, it gets discarded.

huangapple
  • 本文由 发表于 2020年3月15日 23:48:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/60694666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定