JAVA

Basic Methods of JAVA (1)

공부하는기야미 2023. 12. 21. 16:48

 

This post will provide functions and examples of basic methods of JAVA

 

print(), println() method

System.out.print(data)

   - a print method doesn’t add a new line character to the output

System.out.println(data)

   - This method displays the string parameter on the console.

   - It then terminates the output line so that each call to println displays its output on a new line

 

random() method

Math.random()

   - 0 – 0.9999… return a random floating point number that is between 0(inclusive) and 1(exclusive).

Example

n = (int)(11 * Math.random());

   - random() : 0 ~ 0.9999… interval

   - 11 * Math.random()) : 0 ~ 10.9999… interval

   - (int) : get integer part

   - make a random integer between 0 and 10

n = (int)(11 * Math.random()) – 5;

   - make a random integer between -5 and 5

 

For Loop

for ( counter initialization ; condition ; update counter)

   - The first slot of the for statement usually holds the counter initialization.

   - The second slot gives the condition which will be tested before each new pass through the loop.

   - The third slot explains how to update the counter.

 

Example

for( i = 1 ; i < 20 ; i++) 

   - For i = 1,2,3,….,19, is repeatedly executed 9 times

   - If i = 20, for loop is terminated.

for(i = 2 ; i < 10 ; i+=3)

   - For i = 2,5,8, is repeatedly executed 3 times

   - If i=11, for loop is terminated.

'JAVA' 카테고리의 다른 글

Basic Methods of JAVA (2)  (0) 2023.12.22
Introduction to JAVA  (0) 2023.12.21