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?

paiza問題集でつまずいた問題の備忘録をまとめていきます。

3つのデータの入力 C#編(paizaランク D 相当)

問題はこちらから▶︎3つのデータの入力 C#編(paizaランク D 相当)

◼️つまずいたところ

  • 入力されたスペースを改行に変換
    • line.Replace()で半角スペースを改行に変換

以下は、正解となったコードです。

c#
using System;

class Program
{
    static void Main()
    {
        string line = Console.ReadLine();
        string output = line.Replace(" ", "\n");
        Console.WriteLine(output);
    }
}

N個のデータの入力 C#編(paizaランク C 相当)

問題はこちらから▶︎N個のデータの入力 C#編(paizaランク C 相当)

c#
using System;

class Program
{
    static void Main()
    {
        int N = int.Parse(Console.ReadLine());
        string output;
        if (1<=N<=10)
        {
            for(i=1; 1<=i<=N; i++)
            {
                output = Console.ReadLine();
                Console.WriteLine(output);
            }
        }
    }
}

CS0019、CS0103のエラーコードが出力されました。

◼️つまずいたところ

  • N個の文字列を配列として取得
    • .Split()で任意の文字で区切り、配列に格納
  • 出力ループの記述
    • foreach()で配列の要素がなくなるまでループ

以下は、正解となったコードです。

c#
using System;

class Program
{
    static void Main()
    {
        //Nを取得
        int N = int.Parse(Console.ReadLine());
        //半角スペースで区切り、N個の文字列を取得
        string[] words = Console.ReadLine().Split(' ');
        //各文字列を改行区切りで出力
        foreach(string word in words)
        {
            Console.WriteLine(word);
        }
    }
}
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?