LoginSignup
4

More than 3 years have passed since last update.

【プログラミング学習】 言語別ロジック比較 part.2(変数:数値、文字列)

Last updated at Posted at 2019-09-01

文字列(String)、数値(Int)の宣言と、それを同時に出力する処理の確認まで。

2.変数

Ruby

sample.rb
# コメント行は"#"で記述
moji = "ABC" #文字列
suji = 123 #数値

# この書き方はNG
# puts moji + suji

# この書き方はOK
puts moji + suji.to_s
$ ruby sample.rb
ABC123

Python3

sample.py
moji = "ABC"
suji = 123

# この書き方はNG
# print(moji + suji)

# この書き方はOK
print(moji + str(suji))
$ python test.py
ABC123

Swift

sample.swift
import Foundation
var moji = "ABC"
var suji = 123

// この書き方はNG
//print(moji + suji)

// この書き方はOK
print(moji + String(suji))
(playground)
ABC123

Java

sample.java
package sample;

public class Main {
    public static void main(String[] args) {
        String moji = "ABC";
        int suji = 123;

        // Javaはこの書き方でOK
        System.out.println(moji + suji);
    }
}
(eclipse console)
ABC123

C#

sample.cs
using System;
public class HelloWorld {
    static public void Main () {
        String moji = "ABC";
        int suji = 123;

        // C#もこの書き方でOK
        Console.WriteLine(moji + suji);
    }
}

(手元の実行環境では'mono'を使って実行)

$ mcs sample.cs
(コンパイル成功)
$ mono sample.exe
ABC123

学習してる内容はもっと先行ってるので、どんどんアウトプットしたい。

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
What you can do with signing up
4