gpt4 book ai didi

java - 使用 Java 在文本文件中查找元音最连续的单词

转载 作者:行者123 更新时间:2023-12-01 12:32:05 25 4
gpt4 key购买 nike

我在 Java 中遇到了一个问题,除了最基本的代码之外,必须不用任何东西来解决它。它不能包含数组,而且除了代码中显示的内容之外,我无法真正导入任何内容。问题是这样的:

本书网站上的文件 Words.txt 包含 87,314 个英语单词。编写一个程序来读取该文件并找到具有最多连续元音的单词。

我是编程新手,所以我对要做什么有一些想法,但不知道如何将它们组合在一起。我真的很困惑这个问题。任何帮助将不胜感激。

这是我的想法,但它显然是不正确的,而且我已经花了很多时间在上面,包括在这里和其他地方进行研究,并尝试我找到的代码。我不期望有人为我做作业,但如果您能给我一些指导,我将非常感激。这是我到目前为止所拥有的:

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

最佳答案

这是我的解决方案。但是,如果您想使用我的代码中的注释作为建议,您也应该尝试提出自己的注释来进行练习。

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;
}
}

关于java - 使用 Java 在文本文件中查找元音最连续的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25858770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com