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

我没有得到正确的输出......在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);

  }

}


鸿蒙传说
浏览 136回答 3
3回答

桃花长相依

在您的 while 块中(在 {} 对之后while的行)中,您有某人输入的行。它是字符串类型。如果你在 Java 中查找 String 类,你会发现它有一个 for 的方法length(),所以这就是你获取行长的方法(line.length()返回一个 int 长度)。要跟踪最长的行,您需要在 where countis declared 的地方声明一个变量,该变量将存储输入的最长行。对于每条线,将您拥有的线的长度与迄今为止遇到的最长长度进行比较;如果当前是最长的,则存储它的长度(和它的值,如果你也需要的话,在一个声明在 count 和最长行值旁边的变量中)。我指出将它们放在哪里的原因是它们需要在 while 循环之外声明,以便您可以在循环完成后引用它们。Shortest 以相同的方式完成,但变量不同。祝你好运 - 如果需要,请发布更多问题!我试图给你足够的信息,让你可以自己编写实际的代码,但很难衡量那是多少。

慕标5832272

它会是这样的:import java.util.Scanner;public class Main {&nbsp; &nbsp; public static void main(String[] args)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; String shortest = String.format("%0" + 10000 + "d", 0).replace("0", "x");&nbsp; &nbsp; &nbsp; &nbsp; String longest = "";&nbsp; &nbsp; &nbsp; &nbsp; while (!(line = input.nextLine()).isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter word:&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += line.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.length() > longest.length())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; longest = line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(line.length() < shortest.length())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shortest = line;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The total sum of the word lengths entered was: " + count + " words. ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The longest word was: " + longest + " with length " + longest.length());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The shortest word was: "&nbsp; + shortest + " with length " + shortest.length());&nbsp; }}

哆啦的时光机

根据遇到的第一个单词设置最小和最大的单词大小。然后继续比较值以确定大小。如果单词大小相同,这也可以处理大小写。public static void main(String[] args) {&nbsp; &nbsp; String line;&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; int largestSize = 0;&nbsp; &nbsp; int smallestSize = 0;&nbsp; &nbsp; String longestWord = "";&nbsp; &nbsp; String shortestWord = "";&nbsp; &nbsp; while (!(line = input.nextLine()).isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter word:&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; //Initialize sizes and words on first round.&nbsp; &nbsp; &nbsp; &nbsp; if (count == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smallestSize = largestSize;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shortestWord = line;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Do the comparisons.&nbsp; &nbsp; &nbsp; &nbsp; if (largestSize <= line.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; largestSize = line.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; longestWord = line;&nbsp; &nbsp; &nbsp; &nbsp; } else if (smallestSize > line.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smallestSize = line.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shortestWord = line;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("The total sum of the word lengths entered was: " + count + " words. ");&nbsp; &nbsp; System.out.println("The longest word was: " + longestWord + " with length " + longestWord.length());&nbsp; &nbsp; System.out.println("The shortest word was: " + shortestWord + " with length " + shortestWord.length());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java