我使用的是Windows 7,我正在使用IntelliJ插件进行gradle。
以下是简单的代码:
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
System.out.print("Input a string: ");
Scanner in = new Scanner(System.in);
String input = in.next();
System.out.println("Method 1: " + doesStringHaveUniqueCharsMethod1(input));
}
private static boolean doesStringHaveUniqueCharsMethod1(String input) {
if (input.length() > 256) {
return false;
}
for (int i = 0; i < input.length(); i++) {
for (int j = 0; j < input.length(); j++) {
if (i != j && input.charAt(i) == input.charAt(j)) {
return false;
}
}
}
return true;
}
}
这是build.gradle:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = "Solution"
run {
standardInput = System.in
}
当我从命令行运行gradle -q run
但是当我运行&#34;运行&#34}时它很有效。使用IntelliJ gradle插件的任务,我得到以下输出:
12:22:45 PM: Executing external task 'run'...
:compileJava
:processResources UP-TO-DATE
:classes
:run
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at Solution.main(Solution.java:9)
Input a string: :run FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command 'C:\Program Files (x86)\Java\jdk1.8.0_05\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.082 secs
Process 'command 'C:\Program Files (x86)\Java\jdk1.8.0_05\bin\java.exe'' finished with non-zero exit value 1
12:22:49 PM: External task execution finished 'run'.
答案 0 :(得分:0)
此可能帮助 - &gt;
apply plugin: 'java'
task runTask(type: JavaExec) {
main = 'Solution'
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
}
defaultTasks 'runTask'