Data Types, Print Methods, Scanner, If, Switch including Pseudo+Flowchart

Variables & Data Types

A variable has a name, a value and a data type. This chart shows all the data types.

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


“int” is used for store integer and most commonly used in programming.


“float” and “double” are for storing real numbers which have a fractional part (i.e decimal part). Double can represent for bits than float.


A boolean data type can hold only two values: true & false. Here is an example:

boolean a = true;
boolean b = false;

A char data type is used to store a single character, such as ‘a’, ‘Z’, ‘9’, ‘&’, ‘$’, etc. The character can be letters, digits, special symbols, and even non graphic symbol(tab). If we want to store the value ‘A’ in a variable x:

char x = 'A';

I input my name “Iris” in variable name which is in String class. String is a class instead of a primitive data type. We can have text enclosed in double quotes as the object of String class.

Print Methods

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()”?

The only difference between the print() and println() methods is that the println() statement positions the cursor onto the next line after printing the desired text while the print() method leaves the cursor on the same line. For examples:

  • If we use print(),
System.out.print("Hello ");
System.out.print("World ");
  • The output would be
Hello World
  • If we use println(),
System.out.println("Hello");
System.out.println("World");
  • The output would be
Hello
World

Let’s have a look of a comparison between println() and printf() first:

System.out.println ("Welcome to "+str);
System.out.printf ("Welcome to %s", str);

The printf() statement is used for displaying text that needs to be formatted. So that we can add formate specifiers in printf().

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

If

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-11-28-%e4%b8%8b%e5%8d%882-04-26屏幕快照 2016-11-28 下午2.07.06.png

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

Switch

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

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

Scanner

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

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

Java Exercises

OddEven

此幻灯片需要JavaScript支持。

PrintDayInWord

此幻灯片需要JavaScript支持。

KeyboardScanner

此幻灯片需要JavaScript支持。

BMI

此幻灯片需要JavaScript支持。

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.

留下评论