Learn the basics of writing java programs through programming that deals with numbers. To learn programming, it is important to understand the concept and learn the minimum rules. Learn the rules as if you were learning a foreign language word. The concept is mathematical or philosophical, so if you don't understand it, remember what is said to be the best.
First, let's create the template used in this chapter.
HelloWorld Let's create a class named package name lessons
class name L1
in the project created by HelloWorld by referring to the procedure for creating a project.
Please describe the following program in the file.
Click the Run button to run the program that adds 1 to 10.
From now on, I will explain how to write java through this program.
L1.java
package lessons;
public class L1 {
public static void main(String[] args) {
int startNum=1;
int endNum=10;
int resultNum=0;
int count=0;
while(startNum<=endNum) {
resultNum=startNum+resultNum;
startNum=++startNum;
++count;
}
startNum=startNum-count;
System.out.println(startNum+"From"+endNum+"The result of adding up to"+resultNum);
}
}
A program is something that a repeating computer does. For example, suppose you have a program that adds from 1 to 100. To be honest, if you can only do this, you can't use anything. But wouldn't it be a little more convenient if there were various versatility, such as being able to add from 3 to 103 with the same program, and being able to add from 15 to 30?
In order to have versatility, it is not good to write an addition program from 1 to 100 like 1 + 2 + 3 + ...
.
If you write like ʻa + (a + 1) + ((a + 1) + 1) + ... , you can add from 3 to 103 just by changing a to 3. A value that cannot be determined as a cut in order to reuse the <b> program in this way is called a variable </ b>. In the example program, the first number is defined as
startNum, the number of additions is defined as ʻendNum
, and the result is defined as resultNum
.
This is an example of the formula ʻa + (a + 1) + ((a + 1) + 1) + ... `, but if you are a human, you can see that the value that goes into a is a number. But computers don't know that. Therefore, it is necessary to tell in advance that the value in a is a number. The spell used to teach you is the type.
Variables are defined in the form type variable name
.
In the example formula above, we define the variable a in the form ʻint a`.
The types that are often used are as follows.
Mold | What to put |
---|---|
int | Numbers |
long | Large numbers |
double | Numbers including decimals |
boolean | Boolean (true false) |
String | letter |
Use =
to set a value for a variable.
//Set a value in the variable startNum that handles numbers
int startNum=1;
//Set the value to the variable message that handles characters
String message=startNum+"From"+endNum+"The result of adding up to"+resultNum;
In java programs, enclose strings in "" (double quotation marks).
You don't have to remember anything, but the symbols used in mathematics such as +,-, /, * are called operators.
I also use +
to easily connect strings other than numbers.
//Variables and character strings are connected and processed into a single character string.
String message=startNum+"From"+endNum+"The result of adding up to"+resultNum;
++ count
I don't think I've seen this in mathematics, but think of it as having the same meaning as count = count + 1
.
I will supplement what I did not touch because I did not know where to explain.
System.out.println ()
is a spell that prints the contents of ()
.//
(two slashes) represents a comment. The description after this is not executed by the program.
It is also possible to write a comment including line breaks with / * comment * /
.○○ is the answer
.Recommended Posts