LoginSignup
0
1

More than 3 years have passed since last update.

備忘録No.4「文字列を取得して、その周りを飾る」[Java]

Last updated at Posted at 2020-10-29

やりたい事

1.任意の文字数の文字列を一つ受け取る
2.受け取った文字列の周囲を"+"で囲うような見た目で出力する

例.「SUN」という文字列を受け取った場合

+++++
+SUN+
+++++

コード

        Scanner sc = new Scanner(System.in);
        String word = sc.nextLine();
        int wordNum = word.length();//単語の文字数
        int lineNum = wordNum + 2;//単語を囲うのに必要な文字数

        Character [][] plus = new Character[3][lineNum];//
        for(int i = 0; i < plus.length; i ++) {
            for(int j = 0; j < plus[i].length; j++) {
                plus[i][j] = '+';
                for(int k = 0; k < word.length(); k++) {
                    int l = k + 1;
                    plus[1][l] = word.charAt(k);//単語の文字をループで一文字ずつ抽出して、囲われるように要素を更新
                }
                System.out.print(plus[i][j]);
            }
            System.out.println("");
        }

感想

かなりゴリ押しな作り方をしてしまっている気がする...。三重ループを使ってみた(というかなってしまった)が、こんな使い方をするのかどうかも分からず使ってしまっている。

0
1
2

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
1