Please read part1, or if you understand the if statement in Java for the time being. Also, when creating a file, do not leave the file name and class name as they are. If you keep the name of the function you use, you will get an error.
This time, on the premise of part1, we plan to handle non-object-oriented parts such as for statements and character input.
Java has for and while. It is even better if you can use the example sentences properly.
First of all, from the for statement.
java/for.java
public class for
{
public static void main(String[] args)
{
System.out.println("1 to terminal~Output 5");
//Int type i that can be used in for statement is defined and used for control
for(int i = 1; i <= 5; i++) //i++Can be re-executed by adding 1 to i when looping with
{
System.out.println(i);
}
}
}
The points to note in the for statement are as follows.
--Understand and use the number of loops yourself --The delimiter is ";" instead of ","
The delimiter is a syntax, so I think it can't be helped. The point is "understand and use by yourself". For example, what if the initial value of int i in the for statement is 0? In this case, we want to output an integer from "1 to 5", so if the initial value is set to 0, 0 will also be output to the terminal. Also, if the condition of "i <= 5" is set to "i <5", 5 will not be output. As you can see, the for statement is convenient, but be aware that if you specify the wrong range, an unexpected error will occur. Although not mentioned in the notes, variables have the concept of ** scope **. This means that variables defined inside ** "{}" (called blocks) can only be used inside that block **. For example, "int i" defined in the for statement in this program causes an error when trying to use "System.out.println (i)" under the for statement outside the for statement.
Then the while statement. It's the same, but note that the judgment is slightly different.
java/while.java
public class while
{
public static void main(String[] args)
{
System.out.println("1 to Terminal~Output 5");
int i = 1; //Define an integer to be used in a while statement
while(i <= 5) //Run all the time during this condition
{
System.out.println(i);
i++; //Otherwise it will loop infinitely
}
}
}
The points to note in the while statement are as follows.
--Write the assignment of variables and values to control one by one --Judgment is opposite to for statement
Unlike the for statement, which allows variables to be entered at the time of condition definition, the while statement is written first. (I don't think I can do that, but I don't know.)
** The for statement is "executed until the condition of ..." **, while the ** while statement is "executed all the time" **, so the usage is slightly different from the for statement. .. As a specific example, when writing a program that lets you buy juice at a vending machine, if it is a while statement, you can easily apply a program that lets you buy juice until you run out of money. (Devil)
――How to write and move for sentences --Variable scope --Difference between while statement and for statement
There are two ways to enter characters in Java (for example, to calculate an integer) and put them in a variable. First from the first.
java/Scanner.java
import java.util.Scanner; //Required to use Scanner
public class Scanner
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Prepare a Scanner with the name "sc"
System.out.println("Enter something");
String str = sc.next(); //Substitute for string type str for the time being
/*int aaaa = sc.nextInt(); //Use this description for int*/
System.out.println("What you entered" + str);
sc.close(); //Type a command to exit after using sc
}
}
The function that appears here is "Scanner", which assigns the input character string to String. It can also be converted to an integer type or a real number type. The first "import" part is the part that must be written in order to call the function, without which the Scanner cannot be used. The function is "because I didn't make it myself". Some of them are prepared as the basic syntax of Java without importing, so you can code them.
There is another standard input method, so I will also describe it here.
java/BufferedReader1.java
import java.io.*; //java.Load all functions in io(It's just annoying to write more than one)
public class BufferedReader
{
public static void main(String[] args)throws IOException //Processing to throw an exception
{
BufferedReader br = /*Declare "br" of BufferedReader type*/
new BufferedReader(new InputStreamReader(System.in)); //One line is fine, but it's hard to see, so line breaks
String str = br.readLine(); //Assign keyboard input to str as String type
/*int aaaa = Integer.parseInt(str); //Insert and use this statement when converting to int*/
System.out.println(str); //Output to Terminal
br.close(); //Described just in case
}
}
In Java, "Scanner" and "Buffered Reader" can be used, and it is troublesome to write, but Buffered Reader is faster. When using either Scanner or BufferedReader, be aware that a compile error will occur unless you first write an import statement to make it usable. Also, write a process that ends when you use it ** like "sc.close ();". Same as cleaning up in reality. Exceptions will be described at a later date. For the time being, keep it here with "this kind of thing". Both are only partially written, so please refer to the official documentation. It's all listed
--How to use Scanner and BufferedReader --Each ending procedure
Recommended Posts