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?

【#1】C言語未経験がC#を触ってみた - 値の入出力

Posted at

0. 参考にさせていただいているサイト様

C# によるプログラミング入門
[基礎] 値の入出力
https://ufcpp.net/study/csharp/st_consoleio.html

1. 今回のゴール

「まずは慣れろ」ということで、とりあえず今は詳しい説明省略。

とのことなので、
・説明をひとまず飛ばして、入出力が出来るようにする。

2. 入力

値を受け取りたい時は、 Console.readLine() を用いる。

var str = Console.ReadLine(); 

int.Parseを用いることで、ユーザーが入力した値を整数に変換して変数に代入することができる。また、double.Parseを用いることで、実数として扱うことができる。

var n    = int.Parse(Console.ReadLine());  
var x = double.Parse(Console.ReadLine());

3. 出力

値を出力したい時は、 Console.WriteLine() を用いる。
(前回の記事でも触ったものになります)

int m = 1, n = 3;
Console.Write("m = {0}, n = {1}", m, n); 

【フォーマット出力】
・Console.Writeの第2引数mは0番目、第3引数のnは1番目の引数として {0}, {1} に対応します。
・"{0}"と記述することで、{0}の値を文字として表現できます。

using System;

public class Hello{
    public static void Main(){
        var answer = 9;
        Console.WriteLine("3 x 3 = {0}", answer);
    }
}

「3 x 3 = 9」が無事出力できました!

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?