简体   繁体   English

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

[英]Using JAVA - How can I compute the total sum of input strings & their lengths without using Array?

I am not getting the correct output... Any help with this function in JAVA? 我没有得到正确的输出... JAVA中此功能的任何帮助吗?

The expected Output should be: 预期输出应为:

The total sum of the word lengths entered was: 9 (depending on user input) 输入的单词长度的总和为:9(取决于用户输入)
The longest word was: Oranges, with length 7 最长的词是:橘子,长7
The shortest word was: Ox, with length 2 最短的单词是:Ox,长度为2

Note: No Array is to be used. 注意:不能使用数组。 Thanks 谢谢


Here's my code: 这是我的代码:

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

In your while block (the lines between the {} pair after while ), you have the line that someone entered. 在你而块(后{}对之间的线路while ),你也行,有人进入。 It is of type String. 它是字符串类型。

If you look up the String class in Java, you will find that it has a method for length() , so that's how you get the length of the line ( line.length() returns an int length). 如果在Java中查找String类,您会发现它具有length()的方法,因此这就是获取行的长度的方法( line.length()返回int长度)。

To track the longest line, you need a variable declared up where count is declared that is going to store the longest line entered. 要跟踪最长的行,您需要声明一个变量,该变量在声明count的地方声明,该变量将存储输入的最长的行。 For each line, compare the length of the line you have with the longest length you've encountered so far; 对于每条线,将其长度与迄今为止遇到的最长长度进行比较; if the current one is the longest, then store its length (and its value, if you'll need that also, in a variable declared next to count and the longest line value). 如果当前长度最长,则存储其长度(如果需要,还存储其值,并将其存储在count和最长行值旁边声明的变量中)。 The reason I'm pointing out where to put them is that they need to be declared outside the while loop so that you can refer to them after the loop has finished. 我指出将它们放在何处的原因是,需要在while循环之外声明它们,以便您可以在循环完成后引用它们。

Shortest is done the same way, with different variables. 最短时间以相同的方式进行,但变量不同。

Good luck -- post more questions if you need to! 祝您好运-如果需要,可以发布更多问题! I've tried to give you enough info that you can write the actual code yourself, but it's hard to gauge just how much that is. 我试图为您提供足够的信息,以便您可以自己编写实际的代码,但是很难确定它的数量。

it would be something like this: 会是这样的:

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

Set the smallest and largest word sizes based on the first word encountered. 根据遇到的第一个单词设置最小和最大单词大小。 Then keep comparing values to determine the sizes. 然后继续比较值以确定大小。 This also handles case if words are the same size. 如果单词大小相同,也可以处理这种情况。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM