简体   繁体   English

构建单词列表处理应用程序的初始版本

[英]Build the initial version of a word list processing application

I have a question in regards to the following questions.我对以下问题有疑问。
Now in question 1 down below it wants to create a java class.现在在下面的问题 1 中,它想创建一个 java 类。

  1. Create a Java class called Words with the following contents:创建一个名为 Words 的 Java 类,其内容如下:
import javax.swing.JOptionPane;

public class Words {

public static void main(String[] args) {

WordList ws = new WordList();

 String in = JOptionPane.showInputDialog(

 "Click cancel to end or enter a word and click OK");

 while (in != null) {

ws.addWord(in);

in = JOptionPane.showInputDialog(
 "Click cancel to end or enter a word and click OK");
 }

JOptionPane.showMessageDialog(null, "Word list = " + 

ws.toString());

 JOptionPane.showMessageDialog(null, "First word = " + 

ws.getFirst());

JOptionPane.showMessageDialog(null, "Last word = " + 

ws.getLast());

 }

}

In question 2 it asks the following:在问题 2 中,它询问以下内容:

  1. Create a Java class called WordList in your existing Practical1 project with the following contents:在现有的 Practical1 项目中创建一个名为 WordList 的 Java 类,内容如下:

  2. Right-click in the source code window of the class Words and select "Run File".在 Words 类的源代码窗口中单击鼠标右键,然后选择“运行文件”。 Enter several words, clicking OK after each one and observe how the program behaves.输入几个词,在每个词后单击“确定”并观察程序的行为。
    Make sure you understand how the application works.确保您了解应用程序的工作原理。

  3. In the class WordList, modify the methods getFirs and getLast as described by the associated comments.在类 WordList 中,按照相关注释的描述修改 getFirs 和 getLast 方法。 The associated comments are down below here from the WordList.java class:相关注释位于 WordList.java 类的下方:

import java.util.ArrayList;

public class WordList {

 private ArrayList<String> theWordList = new ArrayList<String>();

public void addWord(String s) {

theWordList.add(s);

    }

 public String getFirst() {

// Replace the return statement below with a statement

 // that returns

 // the first word of theWordList (the word at index 0).

  // Hint: use the ArrayList method "get".

  // If there is no first word (theWordList has no words in it),
         * "-" should be returned.

//

return "junk";

}


public String getLast() {

 // Replace the string "junk" with the

 // last word of theWordList (the word

 // at index size()-1). Hint: use the ArrayList method "get".

 // If there is no last word (theWordList has no words in it),

  // "-" should be returned.

  //

 return "junk";
}

public String toString() {

 return theWordList.toString();
    }
}

The main questions I am more stuck in is with the comments they have left me in steps to completing question 4. How can I do these questions here??我比较纠结的主要问题是他们在完成问题 4 的步骤中给我留下的评论。我如何在这里做这些问题?

In case you need the other class which is the main class, here it is and its labeled Words.java:如果您需要作为主类的另一个类,这里​​是它及其标记的 Words.java:


import javax.swing.JOptionPane;

public class Words {

 public static void main(String[] args) {

  WordList ws = new WordList();

 String in = JOptionPane.showInputDialog(

 "Click cancel to end or enter a word and click OK");

while (in != null) {

  ws.addWord(in);

  in = JOptionPane.showInputDialog(

  "Click cancel to end or enter a word and click OK");
        }

JOptionPane.showMessageDialog(null, "Word list = " + 

ws.toString());

 JOptionPane.showMessageDialog(null, "First word = " + 

ws.getFirst());

JOptionPane.showMessageDialog(null, "Last word = " + 

ws.getLast());


}
}

How to get the contents from an ArrayList如何从 ArrayList 中获取内容

For the first element in public String getFirst() {对于public String getFirst() {的第一个元素

if (theWordList.isEmpty ()) {
    return "-";
return theWordList.get (0);  // first element of List

For the last element in public String getLast() {对于public String getLast() {的最后一个元素

do the same empty check as above and then return做和上面一样的空检查然后返回

using theWordList.size () -1使用theWordList.size () -1

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

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