Java SE向量数组请帮助 [英] Java SE vector array help please

查看:90
本文介绍了Java SE向量数组请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我的Java学习工作需要一些帮助。昨天我在需要Java SE阵列帮助的地方发布了一个与阵列非常相似的问题。请,并设法在大家的帮助下解决该问题。

I need some help here with my java school work. Yesterday I posted one question which is quite similar to this one regarding arrays at Java SE array help needed please and managed to get it solved from the help of you guys.

这回,我需要提示用户输入一系列单词关于数字,然后从那里开始,应用程序将确定所有单词中最长的单词,并打印到控制台以说明最长的单词以及单词的长度。

This time round, I am required to prompt the user to enter a series of words regarding of the number and from there the application will determine the longest word of all and print to console stating the longest word as well as the length of it.

尽管如此,我们没有给出任何提示,但是我认为通过使用vector可能是唯一的解决方案,但是如果我错了,请告诉我。到目前为止,无论用户输入多少字,我都只能打印到控制台,但我不知道如何将每个人添加到向量中,并比较它们的长度。

Although, we weren't given any hint on how to go about it but I thought that by using vector could be the only solution but please advise me if i'm wrong. As for now, I only manage to print to console no matter how many words the user input but I have no idea how to be able to add each individual to the vector and compare them regarding their length.

再次感谢您,希望大家能理解我是编程方面的新手,因此请尽量简化所有内容。 :D

Thank you once again and hope you guys can try to understand that I'm a total newbie in programming so try to keep everything simple. :D

import java.util.*;

class LongestWord2 {
    public static void main(String [] args) {               
        System.out.println("Please enter your words");
        Scanner userInput = new Scanner(System.in);

        Vector <String> v = new Vector <String>();

        while (userInput.hasNext()) {
            v.add(userInput.next());
            System.out.println(userInput.next());
            System.out.println(v.get(0));
        }   
    }
}


推荐答案

如果您想继续从扫描仪获得答复并继续检查最长的名称,则可以使用我在上一个问题中实现的名称进行一些更改。

If you want to continue getting response from your scanner and keep checking for longest name then you can use the one that I implement in your last question with some changes.

示例:

System.out.println("Please enter your words");
    Scanner userInput = new Scanner(System.in);

    Vector<String> v = new Vector<String>();
    String longest = "";
    longest = userInput.nextLine(); //get the first array of words for checking
    v.add(longest);

    while (true) {

        for(String s : v) //iterate to all the array of words
        {
            if(longest.length() < s.length()) //check if the last longest word is greater than the current workd
                longest = s; //if the current word is longer then make it the longest word
        }
        System.out.println("Longest Word: " + longest + " lenght: " + longest.length());

        v.add(userInput.nextLine());
    }

这篇关于Java SE向量数组请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆