Addition, Scanner & Format Specifier

HelloWorld

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%8812-02-34屏幕快照 2016-11-28 上午12.02.56.png


I first input an integer called “num” and put the value of 10 in it. “int” is a data type representing integer.

Addition

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%8812-00-23%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%8812-00-46


This program used the same technique with the previous HelloWorld one. But this time, I used “println()” instead of “printf()”. What is the difference between “print()”, “println()”, and “printf()”? See at Java Exercises

屏幕快照 2016-11-28 下午12.24.41.png

Moon Gravity

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%887-56-17

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%887-57-20

I used Scanner Class in the last three exercises. I first declare the Scanner class of input. Then I asked the user to input his/hers weight. After I got the input, I will put the input back to variable weight. Then after calculation, I output the users’ weight on moon. In the output sentence, I use “%.2f” which represents that my output is a decimal number and I want to keep two decimal spaces.

Scanner Pay

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%8811-53-39%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%8811-53-28

Sum2Num

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%8811-57-16%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8a%e5%8d%8811-57-10

Works Cited

“Displaying Text Using Printf() Method – Java Tutorial – Java With Us”. Javawithus.com. N.p., 2016. Web. 28 Nov. 2016.

“Displaying Text Using Printf() Method – Java Tutorial – Java With Us”. Javawithus.com. N.p., 2016. Web. 28 Nov. 2016.

留下评论