Is there a way that I can cast the variables in my Java program so that I get a double output?

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

Is there a way that I can cast the variables in my Java program so that I get a double output?

问题

public class Challenge1_6
{
   public static void main(String[] args)
   {
      // 1. Declare 3 int variables called grade1, grade2, grade3
      // and initialize them to 3 values
       int grade1 = 78;
       int grade2 = 95;
       int grade3 = 84;

      // 2. Declare an int variable called sum for the sum of the grades
       int sum;
      // 3. Declare a variable called average for the average of the grades
       double average; // Changed data type to double
      // 4. Write a formula to calculate the sum of the 3 grades (add them up).
       sum = grade1 + grade2 + grade3;
      // 5. Write a formula to calculate the average of the 3 grades from the sum using division and type casting.
       average = (double) sum / 3; // Added type casting to ensure double division
      // 6. Print out the average
       System.out.println(average);
   }
}
英文:

I am a beginner in Java and I am being asked to write a program to find the average of three grades. I am trying to figure out how to get a double output by type casting but I don't know where to cast. I have already written some code myself but the grader is still saying I'm not getting the right answer.

Here are the program instructions:

> In the code below, type in three made up int grades and then sum and
> average them. Use casting to report the result as a double. For
> example, if the grades are 90, 100, and 94, the sum of the three
> numbers is 90 + 100 + 94 = 284, and the average is the sum 284 divided
> by 3 which casted to a double is 94.666667. You should use your
> variables instead of the numbers in your formulas. Follow the
> pseudocode below.
>
> Type in three made up int grades and then sum and average them. Use
> type casting to report the result as a double.

Here is my code:

public class Challenge1_6
{
   public static void main(String[] args)
   {
      // 1. Declare 3 int variables called grade1, grade2, grade3
      // and initialize them to 3 values
       int grade1 = 78;
       int grade2 = 95;
       int grade3 = 84;

      // 2. Declare an int variable called sum for the sum of the grades
       int sum;
      // 3. Declare a variable called average for the average of the grades
       int average;
      // 4. Write a formula to calculate the sum of the 3 grades (add them up).
       sum = grade1 + grade2 + grade3;
      // 5. Write a formula to calculate the average of the 3 grades from the sum using division and type casting.
       average = sum / 3;
      // 6. Print out the average
       System.out.println(average);
   }
}

This is my output (It wants a decimal but I don't know how to get it):

enter image description here

答案1

得分: 4

好的,以下是翻译好的内容:

平均值的变量必须是双精度浮点型
并且你需要将除法结果强制转换以适应平均值变量

        double average;
        // 4. 编写一个公式来计算3个成绩的总和(将它们相加)。
        sum = grade1 + grade2 + grade3;
        // 5. 编写一个公式来通过除法和类型转换从总和计算3个成绩的平均值。
        average = (double) sum / 3;
        System.out.println(average);
英文:

Well the average variable must be double
and you cast the division result to fit in the average variable

    double average;
    // 4. Write a formula to calculate the sum of the 3 grades (add them up).
    sum = grade1 + grade2 + grade3;
    // 5. Write a formula to calculate the average of the 3 grades from the sum using division and type casting.
    average = (double) sum / 3;
    System.out.println(average);

答案2

得分: 3

只需将变量"average"更改为"double"。

double average = Double.valueOf(sum / 3);

逻辑是:
函数中至少有一个变量(a/b)应为double类型
或者我们需要根据需要将int值转换为Double。

英文:

Simply change the variable "average" to double.

double average=Double.valueOf(sum / 3);

Logic is:
At least one variable in the function (a/b) should be the type of double
Or we need to convert the int value to Double as required.

答案3

得分: 0

你可以直接通过双精度字面值 `3.0` 或 `3d` 进行除法运算,以执行浮点除法,而不是整数除法。

双精度变量 average;
sum = grade1 + grade2 + grade3;
average = sum / 3.0;


[演示链接!](https://ideone.com/NViUIh)
英文:

You can simply divide by the double literal 3.0 or 3d so that it performs floating point division instead of instead divison.

double average;
sum = grade1 + grade2 + grade3;
average = sum / 3.0;

Demo!

答案4

得分: 0

只需将平均值转换为 double,以避免由于从 doubleint 的转换而导致任何丢失。

public class Challenge1_6 {
   public static void main(String[] args) {
       int grade1 = 78;
       int grade2 = 95;
       int grade3 = 84;

       int sum;
       double average;
       sum = grade1 + grade2 + grade3;
       average = (double) sum / 3;
       System.out.println(average);
   }
}
英文:

Just convert the average to double to avoid any missing due to the cast from double to int

public class Challenge1_6 {
   public static void main(String[] args) {
       int grade1 = 78;
       int grade2 = 95;
       int grade3 = 84;

       int sum;
       double average;
       sum = grade1 + grade2 + grade3;
       average = sum / 3;
       System.out.println(average);
   }
}

huangapple
  • 本文由 发表于 2020年7月25日 23:09:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63089942.html
匿名

发表评论

匿名网友

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

确定