'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...
Solution 1:[1]
You may use formated output here:
System.out.printf("%d + %d = %d\n",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 = %f\n",firstnumber,secondnumber,average);
Solution 2:[2]
This is code you are looking for:
System.out.println(firstnumber + " + " + secondnumber + " = " + sum);
Code for average:
double average = (double) sum / 2;
System.out.println(average);
Solution 3:[3]
It basicly means that you have not had a quatation mark some where so the computer dose not know where the end of your sentence is .(or start)
Solution 4:[4]
Write your code below this line ? print("Day 1 - Python Print Function") print(The function is declared like this:) print("print('what to print')") . Can you see where you put the qutation mark?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Chau |
Solution 4 | Chau |