Please forgive it as it is a self-sufficient memorandum.
There are only three movements in the program. ① Progress ② Conditional branch ③ Repeat
Greeting.java
class Greeting {
public static void main(String args[]) {
System.out.println("Good morning");
System.out.println("Good afternoon");
System.out.println("Good evening");
}
}
Terminal
$ javac Greeting.java
$ java Greeting
Good morning
Good afternoon
Good evening
It seems that javac Greeting.java creates a folder called Greeting.class.
Array.java
class Array {
public static void main(String[] args) {
String[] arr = {"sato", "suzuki", "takahashi"};
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
}
}
Terminal
sato
suzuki
takahashi
For.java
class For {
public static void main(String[] args) {
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
System.out.println(i + "-" + j);
}
}
}
}
Terminal
0-0
0-1
0-2
1-0
1-1
1-2
2-0
2-1
2-2
Since it is nested, it will be processed like this.
Let's create a function to calculate the average salary of a salaryman for 5 months and judge whether it is amazing or not.
Lesson.java
class Office_worker {
String name;
public int calculateAVG(int[] data) {
int sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i];
}
int avg = sum / data.length;
return avg;
}
public String judge(double avg) {
String result;
if (avg >= 250000) {
result = "Wow!";
} else {
result = "not yet!";
}
return result;
}
}
public class Lesson {
public static void main(String[] args) {
Office_worker a001 = new Office_worker();
a001.name = "sato";
int[] data = {270000, 265000, 250000, 290000, 230000};
int avg = a001.calculateAVG(data);
String result = a001.judge(avg);
System.out.println(avg);
System.out.println(a001.name + "The average salary is" + result);
}
}
Terminal
261000
The average salary of sato is amazing!
Recommended Posts