我是Java新手,我正在努力让程序运行来解决和显示 方程的输出。我试过改变了地方 括号,并将方程式放在代码的其他部分,但我得到0 作为输出或错误的答案。任何智慧的话语都会很大 赞赏。
import java.util.*;
import static java.lang.Math.*;
public class JFirstTest
{
public static void main(String[] args)
{
// Declare variables and equations
int W, X, Y, Z;
W = 10;
X = 20;
Y = 30;
Z = 40;
int FormulaOne = (W + X) / (Y + Z);
int FormulaTwo = (X + ( Y / W)) / Z;
int FormulaThree = X * (W - Y)/ (X * Y - Z);
// System.out.println formulas
System.out.println("\n\tWhen W=" + W + " X=" + X + " Y=" + Y + " Z=" + Z + " then FormulaOne = " + FormulaOne);
System.out.println("\n\tWhen W=" + W + " X=" + X + " Y=" + Y + " Z=" + Z + " then FormulaTwo = " + FormulaTwo);
System.out.println("\n\tWhen W=" + W + " X=" + X + " Y=" + Y + " Z=" + Z + " then FormulaThree = " + FormulaThree);
System.out.println("\n");
Update: This has been marked as Duplicate, however, I did searches before I
posted this question and the answers eluded me...Also changed int to double
and now it works fine.
答案 0 :(得分:2)
将整数除以整数将导致整数,并向下舍入(截断)。因此,将较小的数字除以较大将导致零,因此您可能需要考虑将变量类型更改为float / double。
答案 1 :(得分:2)
这样想吧......如果一个int(整数)是一个整数,那么如果你除以10乘20,那么你的程序只会给你0,因为0.5不是整数(所以它向下舍入)。
您将使用什么原始数据类型来计算任何小数?
答案:Double