0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javaのswitch文

Posted at

##Javaを使った条件分岐

###if文を用いると


class Main {
  public static void main(String[] args) {
    int number = 12;

  if(number % 3 == 0){
        System.out.println("3で割り切れます");
  else if(number % 3 == 1){
        System.out.println("3で割ると1余ります");
  else
        System.out.println("3で割ると2余ります");
  }

 }
}

###swich文を用いると

※ 各caseの最後に、break;をつけるのを忘れずに!
 忘れると、合致したcaseの処理を行った後、その次のcaseの処理も実行してしまう。

class Main {
  public static void main(String[] args) {
    int number = 12;
    switch(number % 3){
      case 0:
        System.out.println("3で割り切れます");
        break;
      case 1:
        System.out.println("3で割ると1余ります");
        break;
      case 2:
        System.out.println("3で割ると2余ります");
        break;
    }
    
  }
}
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?