如何显示等式?

时间:2015-02-17 14:32:46

标签: java equation multiplying

import java.text.DecimalFormat;
import java.util.*;

public class Practice {
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("#,###,##0.0000"); 
        float myfloat1 =0;
        float myfloat2 =0;
        float myfloat3 =0;
        float myfloat4 =0;
        float result =0; 
        result = (myfloat1 * myfloat2)/(myfloat3 + myfloat4); 

        System.out.println("Please enter first multiplied number");
        myfloat1 = console.nextFloat();

        System.out.println("Please enter second multipled number");
        myfloat2 = console.nextFloat();

        System.out.println("Please enter first addition number");
        myfloat3 = console.nextFloat();

        System.out.println("Please enter second addition number");
        myfloat4 = console.nextFloat();

        System.out.println("Your end result is " + df.format(result));
    }
}
  

请输入第一个相乘的数字
  1
  请输入第二个倍数   1
  请输入第一个加号
  1
  请输入第二个加号
  1
  你的最终结果是?

我也在最后得到一个问号。我目前遇到的问题是无法显示公式和用户指定的数字。如(1 * 1)/(1 + 1)并显示其结果。具有4位小数精度。

2 个答案:

答案 0 :(得分:0)

试试这个;

public static void main(String[] args) {

    DecimalFormat df = new DecimalFormat("#,###,##0.0000");

    float myfloat1 = 0;

    float myfloat2 = 0;

    float myfloat3 = 0;

    float myfloat4 = 0;

    float result = 0;



    System.out.println("Please enter first multiplied number");

    myfloat1 = console.nextFloat();

    System.out.println("Please enter second multipled number");

    myfloat2 = console.nextFloat();

    System.out.println("Please enter first addition number");

    myfloat3 = console.nextFloat();

    System.out.println("Please enter second addition number");

    myfloat4 = console.nextFloat();
    result = (myfloat1 * myfloat2) / (myfloat3 + myfloat4);

    System.out.println("Your end result is " + df.format(result));

}

输出是;

Please enter first multiplied number
1
Please enter second multipled number
1
Please enter first addition number
1
Please enter second addition number
1
Your end result is 0,5000

答案 1 :(得分:0)

在您使用用户的浮点数之前,您已经完成了计算。

您想要移动执行计算的语句。所以删除你的陈述,如:

result = (myfloat1 * myfloat2) / (myfloat3 + myfloat4);

将其移动到您拥有用户所有号码的位置,然后打印您想要的任何内容:

result = (myfloat1 * myfloat2) / (myfloat3 + myfloat4);
System.out.println("Your end result for (" + myfloat1 + "*" + myfloat2 + ") / (" + myfloat3 + "  + " + myfloat4 + ") is " + df.format(result));