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

使用 JAVA - 如何在不使用数组的情况下计算输入字符串的总和及其长度?

使用 JAVA - 如何在不使用数组的情况下计算输入字符串的总和及其长度?

鸿蒙传说 2022-06-04 17:40:16
我没有得到正确的输出......在JAVA中这个函数有什么帮助吗?预期的输出应该是:输入的词长总和为:9(取决于用户输入)最长的词是:Oranges,长度为 7最短的词是:Ox,长度为 2注意:不使用数组。谢谢这是我的代码:import java.util.Scanner;public class Main {  public static void main(String[] args)   {    String line;    Scanner input = new Scanner(System.in);     int count = 0;    while (!(line = input.nextLine()).isEmpty())     {      System.out.println("Enter word:  ");      count++;    } System.out.println("The total sum of the word lengths entered was: " + count + " words. ");      System.out.println("The longest word was: " input + " with length " + input.length);      System.out.println("The shortest word was: " input + " with length " + input.length);  }}
查看完整描述

3 回答

?
桃花长相依

TA贡献1860条经验 获得超8个赞

在您的 while 块中(在 {} 对之后while的行)中,您有某人输入的行。它是字符串类型。

如果你在 Java 中查找 String 类,你会发现它有一个 for 的方法length(),所以这就是你获取行长的方法(line.length()返回一个 int 长度)。

要跟踪最长的行,您需要在 where countis declared 的地方声明一个变量,该变量将存储输入的最长行。对于每条线,将您拥有的线的长度与迄今为止遇到的最长长度进行比较;如果当前是最长的,则存储它的长度(和它的值,如果你也需要的话,在一个声明在 count 和最长行值旁边的变量中)。我指出将它们放在哪里的原因是它们需要在 while 循环之外声明,以便您可以在循环完成后引用它们。

Shortest 以相同的方式完成,但变量不同。

祝你好运 - 如果需要,请发布更多问题!我试图给你足够的信息,让你可以自己编写实际的代码,但很难衡量那是多少。


查看完整回答
反对 回复 2022-06-04
?
慕标5832272

TA贡献1966条经验 获得超4个赞

它会是这样的:


import java.util.Scanner;

public class Main {

    public static void main(String[] args) 

    {

        String line;

        Scanner input = new Scanner(System.in); 

        int count = 0;

        String shortest = String.format("%0" + 10000 + "d", 0).replace("0", "x");

        String longest = "";


        while (!(line = input.nextLine()).isEmpty()) {


            System.out.println("Enter word:  ");

            count += line.length();


            if (line.length() > longest.length())

                longest = line;

            if(line.length() < shortest.length())

                shortest = line;

        } 

        System.out.println("The total sum of the word lengths entered was: " + count + " words. ");

        System.out.println("The longest word was: " + longest + " with length " + longest.length());

        System.out.println("The shortest word was: "  + shortest + " with length " + shortest.length());

  }

}


查看完整回答
反对 回复 2022-06-04
?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

根据遇到的第一个单词设置最小和最大的单词大小。然后继续比较值以确定大小。如果单词大小相同,这也可以处理大小写。


public static void main(String[] args) {

    String line;

    Scanner input = new Scanner(System.in);

    int count = 0;

    int largestSize = 0;

    int smallestSize = 0;

    String longestWord = "";

    String shortestWord = "";


    while (!(line = input.nextLine()).isEmpty()) {

        System.out.println("Enter word:  ");

        count++;


        //Initialize sizes and words on first round.

        if (count == 1) {

            smallestSize = largestSize;

            shortestWord = line;

        }


        //Do the comparisons.

        if (largestSize <= line.length()) {

            largestSize = line.length();

            longestWord = line;

        } else if (smallestSize > line.length()) {

            smallestSize = line.length();

            shortestWord = line;

        }

    }


    System.out.println("The total sum of the word lengths entered was: " + count + " words. ");

    System.out.println("The longest word was: " + longestWord + " with length " + longestWord.length());

    System.out.println("The shortest word was: " + shortestWord + " with length " + shortestWord.length());

}


查看完整回答
反对 回复 2022-06-04
  • 3 回答
  • 0 关注
  • 135 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号