JavaとC#の比較4
Java講座7章で比較
SP7_1(for文)
 JavaC#
1
2
3
4
5
6
7
8
class Rei7_1{
    public static void main(String[] args){
        int i;
        for(i=0; i<10; i++){
            System.out.println("連続で出力");
        }
    }
}
class Rei7_1{
    static void Main(){
        int i;
        for(i=0; i<10; i++){
            System.Console.WriteLine("連続で出力");
        }
    }
}

SP7_2(while文)
 JavaC#
1
2
3
4
5
6
7
8
9
class Rei7_2{
    public static void main(String[] args){
        int i = 0;
        while(i<10 ){
            System.out.println("連続で出力");
            i++;
        }
    }
}
class Rei7_2{
    static void Main(){
        int i = 0;
        while(i<10 ){
            System.Console.WriteLine("連続で出力");
            i++;
        }
    }
}

SP7_3(while文の例)
 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
import java.io.*;

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

        System.out.print
            ("-1を入力するまで繰り返す:");
        String str = br.readLine();

        int a = Integer.parseInt(str);

        //-1でないとき繰り返す
        //-1を入力したときは終了する
        while(a != -1){
            System.out.print
                ("-1を入力するまで繰り返す:");
            str = br.readLine();

            a = Integer.parseInt(str);
        }
    }
}
using System;

class Rei7_3{
    static void Main(){
        Console.Write
            ("-1を入力するまで繰り返す:");
        string str = Console.ReadLine();

        int a = Convert.ToInt32(str);

        //-1でないとき繰り返す
        //-1を入力したときは終了する
        while(a != -1){
            Console.Write
                ("-1を入力するまで繰り返す:");
            str = Console.ReadLine();

            a = Convert.ToInt32(str);
        }
    }
}

SP7_4(do-while文の例)
 JavaC#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.io.*;

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

        String str;
        int a;
        
        //-1でないとき繰り返す
        //-1を入力したときは終了する
        do{
            System.out.print
                ("-1を入力するまで繰り返す:");
            str = br.readLine();
            a = Integer.parseInt(str);
        }while(a != -1);
    }
}
using System;

class Rei7_4{
    static void Main(){
        string str;
        int a;
        
        //-1でないとき繰り返す
        //-1を入力したときは終了する
        do{
            Console.Write
                ("-1を入力するまで繰り返す:");
            str = Console.ReadLine();
            a = Convert.ToInt32(str);
        }while(a != -1);
    }
}

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