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

时间:2019-02-04 19:13:08

标签: java

我没有得到正确的输出... JAVA中此功能的任何帮助吗?

预期输出应为:

  

输入的单词长度的总和为:9(取决于用户   输入)
最长的单词是:橘子,长7
  最短的词是:牛,长度为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 个答案:

答案 0 :(得分:4)

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

如果您在Java中查找String类,您会发现它具有用于length()的方法,因此这就是获取行长的方式(line.length()返回int长度)。

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

最短测试以相同的方式进行,但变量不同。

祝您好运-如果需要,可以发布更多问题!我试图为您提供足够的信息,以便您可以自己编写实际的代码,但是很难确定它的数量。

答案 1 :(得分:0)

是这样的:

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());
  }
}

答案 2 :(得分:0)

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

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());
}