gpt4 book ai didi

Java不识别计算?

转载 作者:行者123 更新时间:2023-12-01 06:52:56 25 4
gpt4 key购买 nike

我是 Java 初学者,迷失了方向。有人可以帮助我吗,我只是想让这段代码正常工作。除了最后的计算之外,一切都运行良好。我已复制并粘贴代码和输出。

import java.util.Scanner;
public class newfile {
public static void main(String[]args) { // starting point of the program
Scanner input = new Scanner(System.in);
int oneA, oneB, oneC;
double finA=0, finB=0, finC=0,average=0;

System.out.println("Hello, Welcome to the 'Calculate Your Grades' program!\nPlease input your predicted grade for Assignment 1A in a percentage format:");
oneA=input.nextInt();
System.out.println("Thank you, can you please input your predictive grade for Assignment 1B?");
oneB=input.nextInt();
System.out.println("Finally, can you please input your predictive grade for Assignment 1C?");
oneC=input.nextInt();
average=(oneA+oneB+oneC)/3;
System.out.println("Excellent, Thank you. The program has calculated your average grade to be "+average+"%");
finA=(oneA/100)*25;
finB=(oneB/100)*25;
finC=(oneC/100)*50;
System.out.println("This module states:\n Assignment 1A is weighted at 25%. You achieved "+finA+"%\n Assignment 1B is weighted at 25%. You achieved "+finB+"%\n Assignment 1C is weighted at 50%. You achieved "+finC+"%");
}
}

输出:

--------------------Configuration: newfile - JDK version 1.7.0_45 <Default> - <Default>--------------------

Hello, Welcome to the 'Calculate Your Grades' program!
Please input your predicted grade for Assignment 1A in a percentage format:
60
Thank you, can you please input your predictive grade for Assignment 1B?
60
Finally, can you please input your predictive grade for Assignment 1C?
60
Excellent, Thank you. The program has calculated your average grade to be 60.0%
This module states:
Assignment 1A is weighted at 25%. You achieved 0.0%
Assignment 1B is weighted at 25%. You achieved 0.0%
Assignment 1C is weighted at 50%. You achieved 0.0%

流程已完成。

构建报告很清晰。

最佳答案

平均值=(oneA+oneB+oneC)/3;

这里也一样:

finA=(oneA/100)*25;
finB=(oneB/100)*25;
finC=(oneC/100)*50;
<小时/>

Dividing integers in a computer program requires special care. Some programming languages, treat integer division (i.e by giving the integer quotient as the answer). So the answer is an integer.

您正在执行整数除法。因此:

60/100 => 0 

您可以通过将其中一个参数设置为 double 来避免这种情况:

finA=(oneA/100.0)*25;
finB=(oneB/100.0)*25;
finC=(oneC/100.0)*50;

关于Java不识别计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20338827/

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