LoginSignup
67
77

More than 1 year has passed since last update.

【Java】Java入門

Last updated at Posted at 2019-04-01

Java使わなきゃいかんかんじなので各種メソッドのメモ用
やっぱPythonでしょ

MacでのJava環境構築

ここで同意・ダウンロード・インストール
ターミナル開いて

java

でエラーなければ環境構築完了

コンパイルと実行方法

まずはこのファイル作ってみてください

Test.java
public class Test{
     public static void main(String[] args){
          System.out.print("hello\n");
     }
}

コンパイル

$ javac Test.java

実行

$ Java Test
出力
hello

変数

VariableTest.java
public class VariableTest{
    public static void main(String[] args){
        int age = 35;
        System.out.print(age+"\n");
    }
}
出力
35

if文

Greeting.java
public class Greeting{
   public static void main(String[] args){
      //int time = 8;
      //System.out.println("只今の時刻は" + time + "時です");
      //System.out.println("おはようございます");
     int time = 8;
     if((time >= 4) && (time <= 10)){
       System.out.println("只今の時刻は" + time + "時です");
       System.out.println("おはようございます");
     }else if((time >= 11) && (time <= 17)){
       System.out.println("只今の時刻は" + time + "時です");
       System.out.println("こんにちは");
     }else if(time == 18){
       System.out.println("只今の時刻は" + time + "時です");
       System.out.println("こんばんは");
     }else if((time >= 19) && (time <= 3)){
       System.out.println("只今の時刻は" + time + "時です");
       System.out.println("おやすみなさい");
     }
   }
}
出力
只今の時刻は8時です
おはようございます

配列(リスト)

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("Aさんの点数:" + scores[0] + "点");
          System.out.println("Bさんの点数:" + scores[1] + "点");
          System.out.println("Cさんの点数:" + scores[2] + "点");
          System.out.println("Dさんの点数:" + scores[3] + "点");
     }
}
出力
Aさんの点数:80点
Bさんの点数:65点
Cさんの点数:70点
Dさんの点数:95点

String

Greeting.java
public class Greeting{
   public static void main(String[] args){
      int time = 8;
      String message = "只今の時刻は" + time + "時です";
      String greeting = "";

      if((time >= 4) && (time <= 10)){
         greeting = "おはようございます";
      }else if((time >= 11) && (time <= 17)){
         greeting = "こんにちは";
      }else if(time == 18){
         greeting = "こんばんは";
      }else if((time>=18 && time<=24) || (time<=3 && time>=0)){
         greeting = "おやすみなさい";
      }else{
         message = "timeには0~24の値を入力してください";
      }

      System.out.println(message);
      System.out.println(greeting);
   }
}
出力
只今の時刻は8時です
おはようございます

for文

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の5乗は、");
          System.out.println(answer);
     }
}
出力
2の5乗は、32

for文でインデックス指定

  1. 配列の長さ出して
  2. 出した配列の長さでforして
  3. 変化するiで要素番号(インデックス)を指定
Scores.java
public class Scores{
     public static void main(String[] args){
          String[] names = new String[]{"青木","飯田","上田","江藤"};
          int[] scores = new int[]{80,65,70,95};

          for(int i=0; i< names.length; i++){
               System.out.println(names[i] + "さんの点数:" + scores[i] + "点");
          }
     }
}
出力
青木さんの点数:80点
飯田さんの点数:65点
上田さんの点数:70点
江藤さんの点数:95点

関数(メソッド)

Pythonと同じで
mainは自動で実行

引数なし

sub(作った関数)は呼び出すことで実行される

MethodTest.java
//coding:utf-8
class MethodTest{
   public static void main(String[] args){
      System.out.println("mainメソッドが呼ばれました");
      sub();
   }
   public static void sub(){
      System.out.println("subメソッドが呼ばれました");
   }
}
出力
mainメソッドが呼ばれました
subメソッドが呼ばれました

引数あり

Nの2乗

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 + "の2倍は、");
          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の" + n + "乗は、");
          System.out.println(answer);
     }
}
出力
5の2倍は、10
65の2倍は、130
3247の2倍は、6494
2の3乗は、8
2の10乗は、1024

戻り値あり

ReturnTest.java
public class ReturnTest{
   public static void main(String[] args){
      int a = 23165247;
      if( isMultipleOf3(a) ){
         System.out.println(a + "は3で割り切れます");
      }else{
         System.out.println(a + "は3で割り切れません");
      }
   }
   public static boolean isMultipleOf3(int n){
      boolean result;
      result = ( n % 3  == 0 );
      return result;
   }
}
出力
23165247は3で割り切れます

戻り値で関数を作る

ReturnTest.java
public class ReturnTest{
   public static void main(String[] args){
           System.out.println(isMultipleOf3(123456789));
     }

    //Stringで返す場合
   public static String isMultipleOf3(int n){
     String resultMessage = "";
     if(n % 3 == 0){
          resultMessage = n + "は3で割り切れます";
     }else{
          resultMessage = n + "は3で割り切れません";
     }
     return resultMessage;
   }
}
ReturnTest.java
public class ReturnTest{
   public static void main(String[] args){
      int a = 23165247;
      if( isMultipleOf3(a) ){
         System.out.println(a + "は3で割り切れます");
      }else{
         System.out.println(a + "は3で割り切れません");
      }
   }
   //True または Falseを返す場合
   public static boolean isMultipleOf3(int n){
      boolean result;
      result = ( n % 3  == 0 );
      return result;
   }
}

ちなみにvoidは戻り値を返さない時に使うもの

出力
123456789は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]);
      }
   }
}

これで実行してみる

$ java CommandLine hello 1 2 3 shuto
結果
hello
1
2
3
shuto

引数に制限なくコマンドラインからの入力に成功

67
77
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
67
77