gpt4 book ai didi

java - 简单方程未返回正确值

转载 作者:行者123 更新时间:2023-12-01 11:06:03 26 4
gpt4 key购买 nike

下面的方程应该输出twosFloat为1,但它返回0.8有人能发现我的错误吗?

int weight = 50;
int barWeight = 45;

int weightWithoutBarWeight = weight - barWeight;

int fortyFives = (weightWithoutBarWeight / 2) / 45;
int thirtyFives = ((weightWithoutBarWeight / 2) - (fortyFives * 45)) / 35;
int twentyFives = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35)) / 25;
int tens = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25)) / 10;
int fives = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10)) / 5;
double twosFloat = ((weightWithoutBarWeight / 2) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10) - (fives * 5)) / 2.5;
int twos = (int)twosFloat;

System.out.println(twosFloat);

最佳答案

您所需要做的就是将每个方程的一些数字转换为浮点型或 double 型。符号“f”将整数转换为 float 。

int weight = 50;
int barWeight = 45;

int weightWithoutBarWeight = weight - barWeight;

float fortyFives = (weightWithoutBarWeight / 2f) / 45f;
float thirtyFives = ((weightWithoutBarWeight / 2f) - (fortyFives * 45)) / 35f;
float twentyFives = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35)) / 25;
float tens = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25)) / 10f;
float fives = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10)) / 5f;
double twosFloat = ((weightWithoutBarWeight / 2f) - (fortyFives * 45) - (thirtyFives * 35) - (twentyFives * 25) - (tens * 10) - (fives * 5)) / 2.5f;
int twos = (int)twosFloat;

System.out.println(twosFloat);

关于java - 简单方程未返回正确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32938004/

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