JavaとC#の比較6
Java講座9章で比較
SP7_1(try〜catch〜finally)
 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
import java.io.*;

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

            System.out.print("1つめの値を入力:");
            String str1 = br.readLine();

            System.out.print("2つめの値を入力:");
            String str2 = br.readLine();

            int num1 = Integer.parseInt(str1);
            int num2 = Integer.parseInt(str2);
            
            num1 = num1 + num2;
            System.out.println
                ("2つの値の和:" + num1);
        }
        catch(NumberFormatException nfe){
            System.out.println
                ("整数を入力してほしいです〜");
        }
        finally{
            System.out.println("終了されたです");
        }
    }
}
using System;

class Rei9_1{
    static void Main(){
        try{
            Console.Write("1つめの値を入力:");
            string str1 = Console.ReadLine();

            Console.Write("2つめの値を入力:");
            string str2 = Console.ReadLine();

            int num1 = Convert.ToInt32(str1);
            int num2 = Convert.ToInt32(str2);
            
            num1 = num1 + num2;
            Console.WriteLine
                ("2つの値の和:" + num1);
        }
        catch(FormatException){
            Console.WriteLine
                ("整数を入力してほしいです〜");
        }
        finally{
            Console.WriteLine("終了されたです");
        }
    }
}

SP9_2(try〜catch〜finally2)
 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
import java.io.*;

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

            System.out.print("1つめの値を入力:");
            String str1 = br.readLine();

            System.out.print("2つめの値を入力:");
            String str2 = br.readLine();

            int num1 = Integer.parseInt(str1);
            int num2 = Integer.parseInt(str2);
            
            num1 = num1 + num2;
            System.out.println
                ("2つの値の和:" + num1);
        }
        catch(NumberFormatException nfe){
            System.out.println
                ("整数を入力してほしいです〜");
        }
        catch(IOException ioe){
            System.out.println("入出力エラーです");
        }
        finally{
             System.out.println("終了されたです");
        }
    }
}
using System;

class Rei9_2{
    static void Main(){
        try{
            Console.Write("1つめの値を入力:");
            string str1 = Console.ReadLine();

            Console.Write("2つめの値を入力:");
            string str2 = Console.ReadLine();

            int num1 = Convert.ToInt32(str1);
            int num2 = Convert.ToInt32(str2);
            
            num1 = num1 + num2;
            Console.WriteLine
                ("2つの値の和:" + num1);
        }
        catch(FormatException){
            Console.WriteLine
                ("整数を入力してほしいです〜");
        }
        catch(System.IO.IOException){
            Console.WriteLine("入出力エラーです");
        }
        finally{
            Console.WriteLine("終了されたです");
        }
    }
}

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