Before reading this, there is java beginner's edition 3, so please have a look there ⬇️⬇︎ ^^
https://qiita.com/shinpachix/items/6f6f972e7980f4c59d3d
continue --Go back to the beginning of the repeat and go to the next lap break --End the iteration and go to the next
int x = o;
while(true) {
//Return here when x is 1(println does not run)
x++;
if(x==1)continue;
System.out.println(x);
if(x==10)break;
}
//When you break, go here
String[] a = new String[3];
a[0] = "Hello";
a[1] = "Good morning";
a[2] = "Goodbye";
for(int i = 0; i < 2; i++) {
System.out.pintln(a[i]);
One variable can be treated like a sequence of multiple variables The inside of [] is called a subscript. Start from 0. The number of subscripts is called a dimension 。 Variables can be used to specify subscripts, so it is convenient to combine with a for statement.
String[][] cell = new String[9][9];
a[0][2] = "Ayumu";
a[5][5] = "Rook";
a[3][3] = "king";
System.out.println(a[5][5]);
//Rook
The dimension can be increased by making the array variable an array. The two-dimensional array can be used to manage shogi and Othello pieces.
The AND operator is&&When&, OR operator||When|There is, but there is a slight difference in behavior.
(When the left side is false in AND or true in OR) Fast because you don't see the right side (use & and | when you want to intentionally execute the right side)
int a = 5;
if(true || a++ > 0) {
System.out.println(a);
}
//5 comes out
if(true | a++ > 0) {
System.out.println(a);
}
//6 comes out
Functions in mathematics
f(x) = 3x+1
f(4) = 13
Performs a fixed operation on the input value.
Functions in the program
public static int hoge(int x) {
return (int) Math.pow(x,2)+3*x+1;
System.out.println(hoge(4));
When a value (argument) is passed, processing is performed and the value (return value) is returned.
Executed when called
Some functions have no arguments in the program
Return value is returned by return (in case of java) Regarding public and static, this time ...
Functions written in a class are called methods. (It's a very rough word, but)
The story of the class itself is coming again ... In java, there is a class and the program is written in it, so it is called a method.
Method definition
public static int hoge(int x) {
return (int)Math.pow(x,2)+3*x+1;
}
Return type method name (argument type argument name, argument type argument name)
If there are multiple arguments, separate them with commas.
If there is no argument, write nothing hoge ()
If there is no value to return, the return type will be void
The place to write is outside the main method, inside the class! (Main is also a method)
For now, let's add static (I will explain in the future)
Method call
hoge(3);
int n = hoge(4);
Call by method name (argument)
If there are multiple arguments, write them separated by commas. The type of the argument to be passed should be the same as the definition
It is also possible to use it for further calculation by assigning the return value to a variable.
You can do the same work together
Example ⬇︎ Put the laundry in ➡️ Move the laundry ➡︎ Dry the laundry
All you have to do is say "wash" all at once
Convenient when executing many times (easy when changing the behavior)
If the scope uses a variable only in the method, it does not affect other parts.
public static int hoge(int x) {
x* = 2;
return x;
}
public static void main(String[] args) {
int a = 3;
int x = 10;
System.out.println(hoge(a)); //6
System.out.println(a); //3(In the case of int, the calculation inside the method does not affect the outside)
System.out.println(a); //10(x in hoge and x in main are different)
}
Recommended Posts