为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

天涯尽头无女友 2023-09-27 16:44:37
抱歉我的问题不清楚,我想输入一行文本并打印出它的长度,然后打印出它的第一个单词,然后打印出文本的其余部分。就像如果输入“I am ruby”,那么输出将是:9I 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);    }}
查看完整描述

3 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

还是一样。


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

    }


}

查看完整回答
反对 回复 2023-09-27
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

我不明白你想要什么。


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

    }


}


查看完整回答
反对 回复 2023-09-27
?
catspeake

TA贡献1111条经验 获得超0个赞

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 通常会向您显示错误


查看完整回答
反对 回复 2023-09-27
  • 3 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信