gpt4 book ai didi

java - 无法使用java多线程计算元音

转载 作者:行者123 更新时间:2023-11-30 08:01:04 25 4
gpt4 key购买 nike

这就是我想要做的:

enter image description here

CountVowel.java

package lab2;


/**
*
* @author Shyam
*/

public class CountVowel implements Runnable {
String[] input;
int vowels = 0;
char ch;

public CountVowel(String[] args) {
input=args;
}

public void run() {
try {
for (int i = 0; i < input.length; i++) {
String s = input[i];

for (int j = 0; j < s.length(); j++) {
ch = s.charAt(j);

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o'
|| ch == 'O' || ch == 'u' || ch == 'U')
vowels++;
}
}
System.out.println("Vowels : " + vowels);

} catch (Exception e) {
}
}

}

元音线程.java

package lab2;


/**
*
* @author Shyam
*/

import java.io.IOException;

public class VowelThread {

public static void main(String[] args)
throws IOException {
for (String str : args) {
System.out.println(str);
}
Thread t1 = new Thread(new CountVowel(args));
t1.start();
}
}

我尝试了如下不同的方法在 cmd 中输入字符串,但是我没有得到想要的结果。

enter image description here

enter image description here

C:\Users\Shyam\Documents\NetBeansProjects\lab2\src>java VowelCounter hello hello see you in
Error: Could not find or load main class VowelCounter


C:\Users\Shyam\Documents\NetBeansProjects\lab2\src>javac -cp . lab2\VowelThread.java hello hello see you in london
error: Class names, 'hello,hello,see,you,in,london', are only accepted if annotation processing is explicitly requested
1 error

非常感谢您的帮助。

最佳答案

你并没有真正做预期的事情,因为你没有为每个提供的参数创建一个新的 Thread,实际上你只为所有参数创建一个 Thread .

要实现它,您可以使用 FutureTask 并让您的类 CountVowel 实现 Callable,如下所示:

public class CountVowel implements Callable<Integer> {
private final String input;

public CountVowel(String input) {
this.input = input;
}

public Integer call() {
int vowels = 0;
// Iterate over all the characters of the input String
for (int j = 0; j < input.length(); j++) {
// Lower case the current character as we need to ignore the case
char ch = Character.toLowerCase(input.charAt(j));
// Use a switch statement instead of an if as it is easier to read and faster
switch (ch) {
case 'a':
case 'e':
case 'o':
case 'i':
case 'u':
vowels++;
break;
}
}
// Returns the sub total
return vowels;
}
}

主类将是这样的:

public class VowelThread {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// Create the list that will contain all the tasks
List<FutureTask<Integer>> futures = new ArrayList<>();
for (String str : args) {
// Create a task for the current String
FutureTask<Integer> future = new FutureTask<>(new CountVowel(str));
// Add the task to the list of tasks
futures.add(future);
// Provide the task to a new Thread and start it
new Thread(future).start();
}
int total = 0;
// Iterate over all the tasks
for (FutureTask<Integer> task : futures) {
// Wait as long as it is needed to get the result of the current task
total += task.get();
}
// Print the total of vowels found
System.out.printf("Number of vowels: %d%n", total);
}
}

关于问题的第二部分,您需要从 src 目录启动您的 javac 命令,否则它无法编译,因为您的类在包 中lab2 这样它应该位于目录 lab2 中,您可以从该目录启动 javac 命令。

因此假设您在 src 中,预期的命令如下:

javac lab2\VowelThread.java 
java lab2.VowelThread Hello Hello see you in Italy in Venice

输出:

Number of vowels: 15

关于java - 无法使用java多线程计算元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38023967/

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