简体   繁体   中英

How do I display the equation?

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));
    }
}

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 ?

I'm also getting a question mark at the end. The problem I currently have is not being able to display the equation and the numbers the user assigns. Such as (1*1)/(1+1) and show the result of it. With 4 decimal precision.

Try like this;

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));

}

Output is;

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

You did the calculation well before you had the floats from user.

You want to move around your statement that does calculation. So remove your statement like:

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

And move it to place where you have all the number from user and then print whatever you want like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM