打印输出输入的长度和单词

抱歉我的问题不清楚,我想输入一行文本并打印出它的长度,然后打印出它的第一个单词,然后打印出文本的其余部分。就像如果输入“I am ruby”,那么输出将是:


9

I

 am ruby

我该怎么做呢?我搜索了这些问题并得到了一些类似的问题,但没有帮助,以下代码是我到目前为止所做的


import java.util.Scanner;

public class lab {

    public static void main(String [] args)

    {

        Scanner kbd = new Scanner(System.in);

        String s = kbd.next();

        String s1 = kbd.nextLine();

        System.out.println(s);

        System.out.println(s1);

    }

}


天涯尽头无女友
浏览 79回答 3
3回答

当年话下

还是一样。import java.util.Scanner;public class Main{    public static void main(String[] args)    {        Scanner kbd = new Scanner(System.in);        System.out.print("Word:");        String s = kbd.nextLine();        System.out.println("Input:" +s); // print the string        System.out.println("Length:" + s.length()); // the length of the string    }}

ABOUTYOU

我不明白你想要什么。import java.util.Scanner;public class lab{    public static void main(String[] args)    {        Scanner kbd = new Scanner(System.in);        System.out.print("1st string: ");        String s = kbd.nextLine();        System.out.print("2nd string: ");        String s1 = kbd.nextLine();        System.out.println(s.length()); // this is for the length        System.out.println(s1); // this if for the input only    }}

catspeake

import java.util.Scanner;public class lab {    public static void main(String [] args)    {        Scanner kbd = new Scanner(System.in);        String s = kbd.next();        String s1 = kbd.nextLine();        System.out.println(s.length());        System.out.println(s);        System.out.println(s1);    }}Scanner没有lenght(),Length()方法仅适用于字符串变量您使用哪个 IDE(Eclipse、Netbeans、Intellij?),因为 IDE 通常会向您显示错误
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java