I have to use Java, so for memos of various methods After all it is Python
Agree, download and install at here Open the terminal
java
If there is no error, the environment construction is completed
Please try to make this file first
Test.java
public class Test{
public static void main(String[] args){
System.out.print("hello\n");
}
}
compile
$ javac Test.java
Run
$ Java Test
output
hello
VariableTest.java
public class VariableTest{
public static void main(String[] args){
int age = 35;
System.out.print(age+"\n");
}
}
output
35
Greeting.java
public class Greeting{
public static void main(String[] args){
//int time = 8;
//System.out.println("The current time is" + time + "It's time");
//System.out.println("Good morning");
int time = 8;
if((time >= 4) && (time <= 10)){
System.out.println("The current time is" + time + "It's time");
System.out.println("Good morning");
}else if((time >= 11) && (time <= 17)){
System.out.println("The current time is" + time + "It's time");
System.out.println("Hello");
}else if(time == 18){
System.out.println("The current time is" + time + "It's time");
System.out.println("Good evening");
}else if((time >= 19) && (time <= 3)){
System.out.println("The current time is" + time + "It's time");
System.out.println("good night");
}
}
}
output
The current time is 8 o'clock
Good morning
Scores.java
public class Scores{
public static void main(String[] args){
//int scoreA;
//int scoreB;
//int scoreC;
//int scoreD;
int[] scores = new int[]{80,65,70,95};
System.out.println("Mr. A's score:" + scores[0] + "point");
System.out.println("Mr. B's score:" + scores[1] + "point");
System.out.println("Mr. C's score:" + scores[2] + "point");
System.out.println("Mr. D's score:" + scores[3] + "point");
}
}
output
Mr. A's score: 80 points
Mr. B's score: 65 points
Mr. C's score: 70 points
Mr. D's score: 95 points
String
Greeting.java
public class Greeting{
public static void main(String[] args){
int time = 8;
String message = "The current time is" + time + "It's time";
String greeting = "";
if((time >= 4) && (time <= 10)){
greeting = "Good morning";
}else if((time >= 11) && (time <= 17)){
greeting = "Hello";
}else if(time == 18){
greeting = "Good evening";
}else if((time>=18 && time<=24) || (time<=3 && time>=0)){
greeting = "good night";
}else{
message = "Enter a value from 0 to 24 for time";
}
System.out.println(message);
System.out.println(greeting);
}
}
output
The current time is 8 o'clock
Good morning
PowerOfTwo.java
//coding:utf-8
public class PowerOfTwo{
public static void main(String[] args){
int n = 5;
int answer = 1;
for( int i=0; i<5; i++ ){
answer = answer * 2;
}
System.out.print("2 to the 5th power");
System.out.println(answer);
}
}
output
2 to the 5th power is 32
Scores.java
public class Scores{
public static void main(String[] args){
String[] names = new String[]{"Aoki","Iida","Ueda","Eto"};
int[] scores = new int[]{80,65,70,95};
for(int i=0; i< names.length; i++){
System.out.println(names[i] + "Score:" + scores[i] + "point");
}
}
}
output
Aoki's score: 80 points
Mr. Iida's score: 65 points
Ueda's score: 70 points
Eto's score: 95 points
Same as Python main runs automatically
*** sub *** (created function) is executed by calling
MethodTest.java
//coding:utf-8
class MethodTest{
public static void main(String[] args){
System.out.println("main method was called");
sub();
}
public static void sub(){
System.out.println("sub method was called");
}
}
output
main method was called
sub method was called
N squared
Calcu.java
//coding:utf-8
public class Calcu{
public static void main(String[] args){
twice(5);
twice(65);
twice(3247);
powerOfTwo(3);
powerOfTwo(10);
}
public static void twice(int n){
System.out.print(n + "Twice as");
System.out.println(n * 2);
}
public static void powerOfTwo(int n){
int answer = 1;
for( int i=0; i<n; i++ ){
answer = answer * 2;
}
System.out.print("2 of" + n + "Multiplication is");
System.out.println(answer);
}
}
output
Twice as 5 is 10
Twice as 65 is 130
Twice as much as 3247, 6494
2 to the 3rd power is 8
2 to the 10th power is 1024
ReturnTest.java
public class ReturnTest{
public static void main(String[] args){
int a = 23165247;
if( isMultipleOf3(a) ){
System.out.println(a + "Is divisible by 3");
}else{
System.out.println(a + "Is not divisible by 3");
}
}
public static boolean isMultipleOf3(int n){
boolean result;
result = ( n % 3 == 0 );
return result;
}
}
output
23165247 is divisible by 3
ReturnTest.java
public class ReturnTest{
public static void main(String[] args){
System.out.println(isMultipleOf3(123456789));
}
//When returning as a String
public static String isMultipleOf3(int n){
String resultMessage = "";
if(n % 3 == 0){
resultMessage = n + "Is divisible by 3";
}else{
resultMessage = n + "Is not divisible by 3";
}
return resultMessage;
}
}
ReturnTest.java
public class ReturnTest{
public static void main(String[] args){
int a = 23165247;
if( isMultipleOf3(a) ){
System.out.println(a + "Is divisible by 3");
}else{
System.out.println(a + "Is not divisible by 3");
}
}
//If it returns True or False
public static boolean isMultipleOf3(int n){
boolean result;
result = ( n % 3 == 0 );
return result;
}
}
By the way, *** void *** is used when no return value is returned.
output
123456789 is divisible by 3
CommandLine.java
public class CommandLine{
public static void main(String[] args){
for(int i=0; i<args.length; i++){
System.out.println(args[i]);
}
}
}
Try running with this
$ java CommandLine hello 1 2 3 shuto
result
hello
1
2
3
shuto
Successful input from the command line with no restrictions on arguments
Recommended Posts