Skip to content
Advertisement

how to print out the user number input with the result instead of showing only result?

I am trying to print out the numbers that user input but i got no idea (example: when user input 2 and 3, it should be show 2+3=5 instead of 5 alone ) also how to find the average of the number too. here is my code:

public static void main(String[] args) {
    Scanner keyboard= new Scanner (System.in);
    System.out.print("Please enter your first number: ");
    int firstnumber= keyboard.nextInt();
    System.out.print("Please enter the second number: ");
    int secondnumber= keyboard.nextInt();
    int sum= firstnumber+secondnumber;
    System.out.println(sum);
    int minus= firstnumber-secondnumber;
    System.out.println(minus);
    int multiply= firstnumber*secondnumber;
    System.out.println(multiply);
    int divide= firstnumber/secondnumber;
    System.out.println(divide);
    int moddivide= firstnumber%secondnumber;
    System.out.println(moddivide);
    **int average= sum/2;**
    System.out.println(average);

    keyboard.close();


}

}

as you can see at (star)(star)…..(star)(star) it shouldn’t be /2 because it doesn’t look like a professional at all…

Advertisement

Answer

You may use formated output here:

System.out.printf("%d + %d = %dn",firstnumber,secondnumber,sum);//<-- prints `3 + 4 = 7`

Formats:

%d, integers
%s, string
%b, boolean
%f, floating

To compute average in decimals:

double average = sum / 2.0;
System.out.printf("Avg of %d & %d = %fn",firstnumber,secondnumber,average);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement