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

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

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

问题

  1. public class Challenge1_6
  2. {
  3. public static void main(String[] args)
  4. {
  5. // 1. Declare 3 int variables called grade1, grade2, grade3
  6. // and initialize them to 3 values
  7. int grade1 = 78;
  8. int grade2 = 95;
  9. int grade3 = 84;
  10. // 2. Declare an int variable called sum for the sum of the grades
  11. int sum;
  12. // 3. Declare a variable called average for the average of the grades
  13. double average; // Changed data type to double
  14. // 4. Write a formula to calculate the sum of the 3 grades (add them up).
  15. sum = grade1 + grade2 + grade3;
  16. // 5. Write a formula to calculate the average of the 3 grades from the sum using division and type casting.
  17. average = (double) sum / 3; // Added type casting to ensure double division
  18. // 6. Print out the average
  19. System.out.println(average);
  20. }
  21. }
英文:

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:

  1. public class Challenge1_6
  2. {
  3. public static void main(String[] args)
  4. {
  5. // 1. Declare 3 int variables called grade1, grade2, grade3
  6. // and initialize them to 3 values
  7. int grade1 = 78;
  8. int grade2 = 95;
  9. int grade3 = 84;
  10. // 2. Declare an int variable called sum for the sum of the grades
  11. int sum;
  12. // 3. Declare a variable called average for the average of the grades
  13. int average;
  14. // 4. Write a formula to calculate the sum of the 3 grades (add them up).
  15. sum = grade1 + grade2 + grade3;
  16. // 5. Write a formula to calculate the average of the 3 grades from the sum using division and type casting.
  17. average = sum / 3;
  18. // 6. Print out the average
  19. System.out.println(average);
  20. }
  21. }

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

enter image description here

答案1

得分: 4

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

  1. 平均值的变量必须是双精度浮点型
  2. 并且你需要将除法结果强制转换以适应平均值变量
  3. double average;
  4. // 4. 编写一个公式来计算3个成绩的总和(将它们相加)。
  5. sum = grade1 + grade2 + grade3;
  6. // 5. 编写一个公式来通过除法和类型转换从总和计算3个成绩的平均值。
  7. average = (double) sum / 3;
  8. System.out.println(average);
英文:

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

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

答案2

得分: 3

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

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

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

英文:

Simply change the variable "average" to double.

  1. 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

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

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

  1. [演示链接!](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.

  1. double average;
  2. sum = grade1 + grade2 + grade3;
  3. average = sum / 3.0;

Demo!

答案4

得分: 0

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

  1. public class Challenge1_6 {
  2. public static void main(String[] args) {
  3. int grade1 = 78;
  4. int grade2 = 95;
  5. int grade3 = 84;
  6. int sum;
  7. double average;
  8. sum = grade1 + grade2 + grade3;
  9. average = (double) sum / 3;
  10. System.out.println(average);
  11. }
  12. }
英文:

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

  1. public class Challenge1_6 {
  2. public static void main(String[] args) {
  3. int grade1 = 78;
  4. int grade2 = 95;
  5. int grade3 = 84;
  6. int sum;
  7. double average;
  8. sum = grade1 + grade2 + grade3;
  9. average = sum / 3;
  10. System.out.println(average);
  11. }
  12. }

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:

确定