2014-06-26 49 views
-1

我需要一些帮助,这里与我的java学校工作。昨天我发布了一个与这个关于Java SE array help needed please的数组非常相似的问题,并设法从你们的帮助下解决它。Java SE矢量阵列帮助

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

虽然我们没有给出任何提示,但我认为通过使用矢量可能是唯一的解决方案,但请告诉我,如果我错了。到目前为止,无论用户输入多少单词,我都只能打印控制台,但我不知道如何能够将每个人添加到矢量中,并根据其长度进行比较。

再次感谢你,希望你们可以试着了解我是编程的全新手,所以尽量保持一切简单。 :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)); 
     } 
    } 
} 
+1

Vector是过时的 - 你应该使用ArrayList的。 – assylias

回答

2

如果你想继续获得来自扫描仪的响应,并保持检查最长的名字,那么你可以使用我在你有了一些变化最后一个问题落实一个。

样本:

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

嘿感谢棒!然而,编译给我留下了.... 请输入你的话 一二三4000 最长的单词:一个长度:3 最长的单词:一个长度:3 最长的单词:三级长度:5 最长文字:三长度:5 最长的单词:千长度:8 –

+0

@ alan_ogz83上面更新见编辑 –

+0

:)它实际上计算所有单词lol的总长度。 –

0

如果你只是需要知道最长的单词,为什么不把远离用户输入的最长单词存储在一个变量中?你不需要一个矢量。

1

你的问题是混乱的,但我认为这

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

旨在更喜欢,

while (userInput.hasNext()) 
{ 
    String line = userInput.next(); 
    line = (line != null) ? line.trim() : ""; 
    v.add(line); 
    // System.out.println(userInput.next()); 
    // System.out.println(v.get(0)); 
    System.out.printf("'%s' is %d letters long%n", line, line.length()); 
} 

并可根据您的问题,这应该足以让你开始。

2

这是最简单的方法。无需将这些单词存储在矢量中。

import java.util.*; 

class LongestWord2 { 
    public static void main(String[] args) { 
     Scanner userInput = new Scanner(System.in); 

     String longestWord = ""; 
     String temp = ""; 
     int longestWordLength = 0; 
     int numOfWords = 10; // num of words to ask from user 
      System.out.println("Please enter " + numOfWords + " words"); 
     for (int i = 0; i<numOfWords ; i++) { // loop for taking words as input 
      temp = userInput.next(); 
      if(temp.length() > longestWordLength){ 
       longestWordLength = temp.length(); 
       longestWord = temp; 
      } 
     } 
     System.out.println("Longest Word = " + longestWord); 
     System.out.println("Longest Word Length = " + longestWordLength); 
     userInput.close(); 
    } 
} 

样本输出:

Please enter 10 words 
a 
ab 
abc 
abcd 
abcde 
abcdef 
abcdefg 
abcdefgh 
abcdefghi 
abcdefghij 
Longest Word = abcdefghij 
Longest Word Length = 10 
+0

嗨嗨感谢您帮助我,但我试过它没有工作后键入单词。 :) –

+0

你怎么测试它?我测试了代码并提供了输出。 –

+0

该代码是为10个单词编写的,即它要求用户输入10个单词。您可以将变量numOfWords的值更改为所需的值。 –