启动应用程序时,它会要求用户一次输入一个单词。输入单词后,将询问用户是否要添加新单词。用户将继续添加单词,直到他们表明已完成为止。
输入所有单词后,应用程序将输出仅列出唯一单词的报告。 (报告中不应出现重复的单词。)
我遇到了找到函数的问题 它应该像那样工作: 程序使其包含关键字列表。将您选择的任何单词放入此列表中(在值中进行硬编码即可)。当您的程序运行时,将用户输入的单词与关键字列表进行比较。在输出报告上指明用户输入的单词是关键字。
另外,我遇到函数排序字符串的问题: 程序,使得单词在输出报告中按字母顺序排列。
该程序适用于显示单词,但它不适用于查找关键字和匹配单词也不适用于排序字母顺序
import java.util.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collection;
public class LabNineKennel {
private InputHelper2 input;
private ArrayList<LabNine> labNineWords;
public void run() {
entry();
displaywords();
findWord();
}
public void entry()
labNineWords = new ArrayList<LabNine>();
input = new InputHelper2();
String word = "";
String more = "";
LabNine newLabNine = null;
// the method to take the information fron the user
while (true) {
word = input.getUserInput(
"Enter the name of the client");
newLabNine = new LabNine();
newLabNine.setWord(word);
labNineWords.add(newLabNine);
// ask the user again if he want to addd any other client
more = input.getUserInput(
"Would you like to enter another?");
if (!more.equals("y")) {
break;
}
}
}
public void displaywords() {
LabNine labnine = null;
System.out.println("\n");
System.out.println("*****Your List Of Unique Word*********");
for (int i = 0; i < labNineWords.size(); i++) {
labnine = labNineWords.get(i);
System.out.println(labnine.display());
}
System.out.println( "\n*******************************************");
}
public void findWord() {
// creat the private instance variable for class Kennel
String [] codes = {"G22", "K13", "I30", "S20"};
LabNine labnine = null;
for (int i = 0; i < labNineWords.size(); i++) {
boolean isValid = false;
labnine = labNineWords.get(i);
if (labnine.equals( codes)) {
isValid = true;
}else isValid = false;
if (isValid = true) {
System.out.println(labnine.display2());
}
}
}
public void sortString()
{
LabNine labnine = null;
System.out.println("\n");
System.out.println("*****Your List Of Unique Word*********");
for (int i = 0; i < labNineWords.size(); i++) {
labnine = labNineWords.get(i);
Arrays.sort(labNineWords);
System.out.println(labnine.display());
}
}
}
LabNine课程
// Creat a client class
public class LabNine {
// creat the private instance variable for class Client
private String word;
private String word1;
private String word2;
private String word3;
private String word4;
/**
* Sets the value of word4.
* @param word4 The value to assign word4.
*/
public void setWord4(String word4) {
this.word4 = word4;
}
/**
* Sets the value of word3.
* @param word3 The value to assign word3.
*/
public void setWord3(String word3) {
this.word3 = word3;
}
/**
* Sets the value of word2.
* @param word2 The value to assign word2.
*/
public void setWord2(String word2) {
this.word2 = word2;
}
/**
* Sets the value of word1.
* @param word1 The value to assign word1.
*/
public void setWord1(String word1) {
this.word1 = word1;
}
/**
* Returns the value of word.
*/
public String getWord() {
return word;
}
/**
* Sets the value of word.
* @param word The value to assign word.
*/
public void setWord(String word) {
this.word = word;
}
// creat the method display to display the data that user input
public String display() {
return
"\n " + getWord();
}
public String display2() {
return "\n" + getWord() + "*";
}
}
答案 0 :(得分:1)
代码行(labnine.equals(codes))正在检查Object是否等于Array。这将返回false。
可以将其修改为以下内容,以检查代码是否包含单词
if ((Arrays.asList(codes).contains(labnine.getWord()))) {
isValid = true;
} else {
isValid = false;
}
答案 1 :(得分:0)
在findWord
中,您应该检查用户输入的字词是否为关键字。您迭代单词(labNineWords
),然后只需执行labnine.equals(codes)
即可查看。您基本上是将LabNine
的实例与字符串数组进行比较。除非你的LabNine.equals
方法是专门编程的,否则它会检查给定的LabNine
实例表示的单词是否包含在数组中(我非常怀疑它是用这种方式编程的),这个{{1调用不会产生你期望的结果。
更简单的方法是创建一组关键字,并简单地检查此集合中是否包含equals
(或者您获得该单词的字符串表示)。一些事情:
labnine.display()
接下来,排序。您使用private static final Set<String> KEYWORDS =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("G22", "K13", "I30", "S20")));
public void findWord() {
for (int i = 0; i < labNineWords.size(); i++) {
LabNine labnine = labNineWords.get(i);
if (KEYWORDS.contains(labnine.display()) {
// This is a keyword
System.out.println(labnine.display2());
}
}
}
排序。这很可能甚至不会编译,因为Arrays.sort
是一个集合,而不是一个数组。您可以使用labNineWords
对此列表进行排序。为此,您的Collections.sort
课程必须实施LabNine
,或者您必须提供Comparable
。假设您要对Comparator
进行排序,可以轻松构建一个带LabNine.display()
另外,您可能不想修改原始列表,因此最好复制一份。
有些事情:
Comparator.comparing(LabNine::display)