Java SE的阵列需要帮助,请 [英] Java SE array help needed please

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

问题描述

我需要一些帮助在这里与我的Java学校工作。
我们被告知要提示用户输入五个字,并从那里他们确定和打印的字最长劝慰最长的单词,以及人物在它的数量。

现在,我只管理通过显示字符数最长使用数组们分开来,但我不知道如何显示这个词本身。是否有人可以帮助我与它,请记住我是在节目一共有新手,我的进步仍然只是在基本那么尽量使其不要太复杂,我请。此外,随意找出那些冗余codeS,因为我知道我有好几个。 :)谢谢!

 进口java.util.Scanner中;
    进口java.util.Arrays中;    类LongestWord
    {
公共静态无效的主要(字串[] args)
{
    扫描仪theinput的=新的扫描仪(System.in);
    的System.out.println(请输入您的五字);    串FWORD = theInput.next();
    字符串剑= theInput.next();
    串TWORD = theInput.next();
    串fhWord = theInput.next();
    串ffWord = theInput.next();    的System.out.println(FWORD +剑+ TWORD + fhWord + ffWord);    INT []的wordCount =新INT [5];    的wordCount [0] = fWord.length();
    的wordCount [1] = sWord.length();
    的wordCount [2] = tWord.length();
    的wordCount [3] = fhWord.length();
    的wordCount [4] = ffWord.length();    Arrays.sort(的wordCount);    的System.out.println(的wordCount [4]);
}
    }


解决方案

您需要的所有字符串添加到数组,遍历所有的人。

示例:

 的String [] =的wordCount新的String [5];    的wordCount [0] = FWORD;
    的wordCount [1] =刀。
    的wordCount [2] = TWORD;
    的wordCount [3] = fhWord;
    的wordCount [4] = ffWord;    字符串最长=;    最长=的wordCount [0]; //获取的字的第一阵列,​​用于检查    对于(一个String:的wordCount)//遍历单词的所有数组
    {
        如果(longest.length()&所述; s.length())//检查是否最后的最长单词比当前workd更大
            最长=秒; //如果当前的字不再然后使它的最长的单词
    }    的System.out.println(最长的单词:+ +最长的长度:+ longest.length());

结果:

 请输入您的五个字
12345
1234
123
12
1
123451234123121
最长的单词:12345长度:5

I need some help here with my java school work. We were told to prompt the user for five words and from there determine the longest word of them and print to console the longest word as well as the number of characters in it.

Right now, I only manage to sort them out using the arrays by displaying the longest number of characters but i'm not sure how to display the word itself. Can someone please help me with it and please bear in mind i'm a total newbie in programming and my progress is still just in the basics so try to make it not too complicated for me please. In addition, feel free to pinpoint those redundant codes as I know I have quite a few. :) Thanks!

    import java.util.Scanner;
    import java.util.Arrays;

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

    String fWord = theInput.next();
    String sWord = theInput.next();
    String tWord = theInput.next();
    String fhWord = theInput.next();
    String ffWord = theInput.next();

    System.out.println(fWord + sWord + tWord + fhWord + ffWord);

    int [] wordCount = new int[5];

    wordCount[0] = fWord.length();
    wordCount[1] = sWord.length();
    wordCount[2] = tWord.length();
    wordCount[3] = fhWord.length();
    wordCount[4] = ffWord.length();

    Arrays.sort(wordCount);

    System.out.println(wordCount[4]);


}
    }

解决方案

You need to add all the string to array and iterate all of them.

sample:

    String [] wordCount = new String[5];

    wordCount[0] = fWord;
    wordCount[1] = sWord;
    wordCount[2] = tWord;
    wordCount[3] = fhWord;
    wordCount[4] = ffWord;

    String longest = "";

    longest = wordCount[0]; //get the first array of words for checking

    for(String s : wordCount) //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());

result:

Please enter your five words
12345
1234
123
12
1
123451234123121
Longest Word: 12345 lenght: 5

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

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