HCB


私信TA

用户名:uq_66496972241

访问量:1274

签 名:

等  级
排  名 3012
经  验 1994
参赛次数 1
文章发表 5
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

首先考虑第一个条件“前N行字符原样输出”,需要使用到输入的nextline()函数,然后输出。再考虑第二个条件需要分割,回车可以用前面的nextline()函数
,空格分割需要用到字符串的分割函数(String类型)+"."split(" ")。


注意事项:输入的N的回车会阻塞nextline的输入(因为nextLine()读取的是nextInt()的回车换行符,导致无法输入)


参考代码:

import java.util.Scanner;


public class Main {

    public static void main(String[]argvs) {

        Scanner sc=new Scanner(System.in);

        int n=sc.nextInt();

        sc.nextLine();

        for(int i=0;i<n;i++)

        {

            String s=sc.nextLine();

            System.out.println(s);

            System.out.println();

        }

        while(sc.hasNextLine())

        {

            String temp=sc.nextLine();

            String[] s=temp.split(" ");

            for(int i=0;i<s.length;i++) {

                System.out.println(s[i]+" ");

                System.out.println();

            }

        }

    }

}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区