JavaとC#の比較3
Java講座6章で比較
SP6_1(if 〜 else文)
 JavaC#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.*;

class Rei6_1{
    public static void main(String[] args) 
    throws IOException{
        BufferedReader br = new
            BufferedReader(
                new InputStreamReader(System.in));

        System.out.print("点数を入力:");
        String str = br.readLine();

        int ten = Integer.parseInt(str);

        if(ten >= 40){
            System.out.println("合格です〜");
        }
        else{
            System.out.println("赤点ですよ・・・");
        }

        System.out.println("報告終了");
    }
}
using System;

class Rei6_1{
    static void Main(){
        Console.Write("点数を入力:");
        string str = Console.ReadLine();

        int ten = Convert.ToInt32(str);

        if(ten >= 40){
            Console.WriteLine("合格です〜");
        }
        else{
            Console.WriteLine("赤点ですよ・・・");
        }

        Console.WriteLine("報告終了");
    }
}

SP6_2(if文)
 JavaC#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.*;

class Rei6_2{
    public static void main(String[] args)
    throws IOException{
        BufferedReader br = new
            BufferedReader(
                new InputStreamReader(System.in));

        System.out.print("点数を入力:");
        String str = br.readLine();

        int ten = Integer.parseInt(str);

        if(ten >= 40){
            System.out.println("合格です〜");
        }

        System.out.println("報告終了");
    }
}
using System;

class Rei6_2{
    static void Main(){
        Console.Write("点数を入力:");
        string str = Console.ReadLine();

        int ten = Convert.ToInt32(str);

        if(ten >= 40){
            Console.WriteLine("合格です〜");
        }

        Console.WriteLine("報告終了");
    }
}

SP6_3(if 〜 else if 〜 else文)
 JavaC#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.*;

class Rei6_3{
    public static void main(String[] args)
    throws IOException{
        BufferedReader br = new
            BufferedReader(
                new InputStreamReader(System.in));

        System.out.print("マンガ何冊もってる?");
        String s = br.readLine();
        
        int satu = Integer.parseInt(s);

        if(satu >= 80){
            System.out.println("すごすぎですね");
        }
        else if(satu >= 60){
            System.out.println("結構あるです〜");
        }
        else if(satu >= 40){
            System.out.println("多いかな");
        }
        else{
            System.out.println("普通かな");
        }
    }
}
using System;

class Rei6_3{
    static void Main(){
        Console.Write("マンガ何冊もってる?");
        string s = Console.ReadLine();
        
        int satu = Convert.ToInt32(s);

        if(satu >= 80){
            Console.WriteLine("すごすぎですね");
        }
        else if(satu >= 60){
            Console.WriteLine("結構あるです〜");
        }
        else if(satu >= 40){
            Console.WriteLine("多いかな");
        }
        else{
            Console.WriteLine("普通かな");
        }
    }
}

SP6_4(switch文)
 JavaC#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.*;

class Rei6_4{
    public static void main(String[] args)
    throws IOException{
        BufferedReader br = new 
            BufferedReader(
                new InputStreamReader(System.in));

        System.out.print("成績を1〜5で入力:");
        
        String str = br.readLine();
        int a = Integer.parseInt(str);
        
        switch(a){
            case 1:
                System.out.println("やばいです");
                break;
            case 2:
                System.out.println("微妙です");
                break;
            case 3:
                System.out.println("普通です");
                break;
            case 4:
                System.out.println("まあ良いです");
                break;
            case 5:
                System.out.println("最高です");
                break;
            default:
                System.out.println("無効です");
                break;
        }
    }
}
using System;

class Rei6_4{
    static void Main(){
        Console.Write("成績を1〜5で入力:");
        
        string str = Console.ReadLine();
        int a = Convert.ToInt32(str);
        
        switch(a){
            case 1:
                Console.WriteLine("やばいです");
                break;
            case 2:
                Console.WriteLine("微妙です");
                break;
            case 3:
                Console.WriteLine("普通です");
                break;
            case 4:
                Console.WriteLine("まあ良いです");
                break;
            case 5:
                Console.WriteLine("最高です");
                break;
            default:
                Console.WriteLine("無効です");
                break;
        }
    }
}

TOPに戻る   JavaとC#の比較に戻る