使用Java在文本文件中查找具有最多连续元音的单词
我在Java中遇到了一个问题,除了最基本的代码外,什么都不能解决。它不能包含数组,除了代码中显示的内容之外,我不能真正导入任何内容。问题是: 该书网站上的words.txt文件包含87314个英语单词。 编写一个程序,读取此文件并查找具有最多连续元音的单词 我对编程是全新的,所以我对做什么有了一些想法,但不知道如何把它们结合起来。我真的被这个问题难住了。任何帮助都将不胜感激 这是我想到的,但它显然是不正确的,我已经花了很多时间在它上面,包括在这里和其他地方研究,并尝试我找到的代码。我不希望有人帮我做作业,但如果你能给我一些指导,我将不胜感激。以下是我目前掌握的情况:使用Java在文本文件中查找具有最多连续元音的单词,java,Java,我在Java中遇到了一个问题,除了最基本的代码外,什么都不能解决。它不能包含数组,除了代码中显示的内容之外,我不能真正导入任何内容。问题是: 该书网站上的words.txt文件包含87314个英语单词。 编写一个程序,读取此文件并查找具有最多连续元音的单词 我对编程是全新的,所以我对做什么有了一些想法,但不知道如何把它们结合起来。我真的被这个问题难住了。任何帮助都将不胜感激 这是我想到的,但它显然是不正确的,我已经花了很多时间在它上面,包括在这里和其他地方研究,并尝试我找到的代码。我不希望有人帮
package vowels;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Vowels
{
public static void main(String[] args)
{
Scanner fileIn = null;
try
{
fileIn = new Scanner(new FileInputStream("words.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
System.exit(0);
}
String eachWord = fileIn.next();
String mostConsecutiveVowels = "";
int w = 0;
int z;
int consecutiveVowels = 0;
int mostConsecutiveVowelsInWord = 0;
int wordWithMostConsecutiveVowels = 0;
boolean vowel;
boolean previousVowel;
boolean mostVowels;
while (fileIn.hasNext())
{
while(consecutiveVowels >= mostConsecutiveVowelsInWord)
{
mostVowels = true;
}
char a = eachWord.charAt(w);
if (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
{
consecutiveVowels++;
consecutiveVowels = mostConsecutiveVowelsInWord;
}
for(z = 1; z <= eachWord.length(); z++)
{
char b = eachWord.charAt(z);
char c = eachWord.charAt(z-1);
while (b=='a'||b=='e'||b=='i'||b=='o'||b=='u')
{
vowel = true;
}
while (c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
{
previousVowel = true;
}
if (vowel = false && previousVowel = true && mostVowels = false;)
{
consecutiveVowels = 0;
}
else if (vowel = false && previousVowel = true && mostVowels = true;)
{
consecutiveVowels = mostConsecutiveVowelsInWord;
}
else if (vowel = true && previousVowel = false)
{
consecutiveVowels = 1;
}
else if (vowel = true && previousVowel = true && mostVowels = true;)
{
consecutiveVowels++;
consecutiveVowels = mostConsecutiveVowelsInWord;
}
else if (vowel = true && previousVowel = true && mostVowels = false;)
{
consecutiveVowels++;
}
}
}
if (mostVowels)
{
if(eachWord.length()>mostConsecutiveVowels.length())
{
mostConsecutiveVowels = eachWord;
}
}
System.out.println("The word in words.txt with the most consecutive vowels is " + mostConsecutiveVowels);
fileIn.close();
}
}
package元音;
导入java.util.Scanner;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
公共类元音
{
公共静态void main(字符串[]args)
{
Scanner fileIn=null;
尝试
{
fileIn=newscanner(newfileinputstream(“words.txt”);
}
catch(filenotfounde异常)
{
System.out.println(“未找到文件”);
系统出口(0);
}
字符串eachWord=fileIn.next();
字符串MOSTCONSECUTIVEVOMELS=“”;
int w=0;
intz;
int连续元音=0;
int mostconceutivevouelsinword=0;
int-wordwithmostconsecutive元音=0;
布尔元音;
布尔元音;
布尔mostVowels;
while(fileIn.hasNext())
{
while(连续元音>=最连续元音)
{
mostVowels=真;
}
字符a=每个字的字符(w);
如果(a='a'| a='e'| a='i'| a='o'| a='u')
{
连续元音++;
连续元音=最连续的元音新词;
}
对于(z=1;z mostConsecutiveVowels.length())
{
大多数连接元音=每个单词;
}
}
System.out.println(“words.txt中最连续元音的单词是”+最连续元音);
fileIn.close();
}
}
这是我的解决方案。但是,如果您想使用我的代码中的注释作为建议,您也应该尝试提出自己的建议以供练习
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Vowels {
public static final String WORD_FILE = "words.txt";
public static void main(String[] args) {
try (Scanner fileScanner = new Scanner(new FileInputStream(WORD_FILE))) {
String targetWord = null; // word with most consecutive vowels
int maxConsecutiveVowels = 0;
while (fileScanner.hasNext()) {
// for each word in the file
String word = fileScanner.next().toLowerCase();
int consecutiveVowels = 0;
for (int i = 0; i < word.length() && i < word.length() - maxConsecutiveVowels + consecutiveVowels; i++) {
// for each character in the word, and exit early if the word is not long enough to beat maxConsecutiveVowels
if (isVowel(word.charAt(i))) {
// consonants reset this to 0
consecutiveVowels++;
} else {
// reached the end of the vowels so check if we beat maxConsecutiveVowels
if (consecutiveVowels > maxConsecutiveVowels) {
maxConsecutiveVowels = consecutiveVowels;
targetWord = word;
}
consecutiveVowels = 0;
}
}
// reached the end of the vowels at the end of the word so check if we beat maxConsecutiveVowels
if (consecutiveVowels > maxConsecutiveVowels) {
maxConsecutiveVowels = consecutiveVowels;
targetWord = word;
}
}
if (targetWord == null) {
System.out.println("there are no words with vowels in " + WORD_FILE);
} else {
System.out.println("the word in " + WORD_FILE + " with the most consecutive vowels is '" + targetWord + "'");
System.out.println("it has " + maxConsecutiveVowels + " consecutive vowels");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static boolean isVowel(char c) {
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
}
return false;
}
}
import java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类元音{
公共静态最终字符串WORD\u FILE=“words.txt”;
公共静态void main(字符串[]args){
try(Scanner fileScanner=new Scanner(new FileInputStream(WORD\u文件))){
字符串targetWord=null;//具有大多数连续元音的单词
int MaxConcertive元音=0;
while(fileScanner.hasNext()){
//对于文件中的每个单词
String word=fileScanner.next().toLowerCase();
int连续元音=0;
for(int i=0;i最大连续元音){
MaxConcertive元音=连续元音;
targetWord=单词;
}
连续元音=0;
}
}
//在单词的末尾到达元音的末尾,所以检查我们是否击败MaxConcertiveVouels
if(连续元音>最大连续元音){
MaxConcertive元音=连续元音;
targetWord=单词;
}
}
if(targetWord==null){
System.out.println(“在”+WORD_文件中没有带元音的单词);
}否则{
System.out.println(““+word_FILE+”中连续元音最多的单词是“'+targetWord+””);
System.out.println(“它有“+MaxConcertive元音+”连续元音”);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}
}
私有静态布尔IS元音(字符c){
开关(c){
案例“a”:
案例“e”:
案例“i”:
案例“o”:
案例“u”:
返回true;
}
返回false;
}
}
对于此类问题,我认为最好先手工解决。然后完成你手工完成的步骤,并将这些步骤输入到程序中。你有一个典型的初学者问题,你的程序中有太多的东西。你需要考虑一下这个问题,在你的算法中只有大约一半的行是真正需要的。我想补充一点,如果你能把你的问题分解成不同的方法,那就更好了。我在这里太新了,以至于我不确定我在自己的帖子中的评论是否正确(这是我关于堆栈溢出的第一个问题),但非常感谢所有回答的人(我只允许标记一个人),非常感谢@nmore提供了非常有用的答案。今晚下课后,我和另一个学生聊天,他说的和markspace说的一样,我把事情弄得太复杂了。所以我尽量去掉所有布尔字母(我用if(c='a'| c='e'| c='I'| c='o'| c='u')来判断这个字符是否是元音,用for和if-else。谢谢哦,不幸的是,我还没有足够的代表投票支持这个答案。我计划,如果我有足够的代表性,减去MaxConcertive元音,再加上ConcertiveV