仅使用过IDE进行Java开发,并想学习如何使用JShell,但是对于基本的hello world示例以及我尝试的其他任何方法,我都遇到了以下错误。不知道“;”在哪里错误来自。
| javac HelloWorld.java
|错误:
| ';'预期
| javac HelloWorld.java;
HelloWorld.java的代码
package allevenproj;
import java.util.*;
public class AllEvenProj {
static int Read(int[] arr) {
Scanner scan = new Scanner(System.in);
int count = 0;
for (int i = 0; i < arr.length; i++) {
System.out.printf("Enter arr[%d]: ", i);
arr[i] = scan.nextInt();
if (arr[i] % 2 == 0) {
count++;
}
}
System.out.printf("There are %d even numbers\n", count);
return count;
}
static int[] GetEven(int[] arr, int count) {
int[] evenArr = new int[count];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < count; j++) {
if (arr[i] % 2 == 0) {
evenArr[j] = arr[i];
}
}
}
return evenArr;
}
static void Print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = scan.nextInt();
int[] arr = new int[n];
int a = Read(arr);
Print(GetEven(arr, a));
}
}
答案 0 :(得分:1)
您没有在JShell中进行编译,可以添加main方法然后调用它
public static void main(String[] args) {
System.out.println("Hello World");
}
}
main(null);
以下示例显示了正在定义的方法并运行该方法:
jshell> String grade(int testScore) { ..... jshell> grade(88)
答案 1 :(得分:1)
以下是两个不同的“ hello world”程序:
rowSums(test[grep("conf", names(test))] == test[grep("chall", names(test))])
thufir@dur:~/jshell$
thufir@dur:~/jshell$ java hello.java
Hello World from Java
thufir@dur:~/jshell$
thufir@dur:~/jshell$ cat hello.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World from Java");
}
}
thufir@dur:~/jshell$
thufir@dur:~/jshell$ jshell hello.jsh
Hello World
thufir@dur:~/jshell$
thufir@dur:~/jshell$ ./hello.jsh
jshell 11.0.1
Hello World
thufir@dur:~/jshell$
thufir@dur:~/jshell$ cat hello.jsh
//usr/bin/env jshell --show-version "$0" "$@"; exit $?
System.out.println("Hello World")
/exit
thufir@dur:~/jshell$
或.jsh
脚本是可执行的,因此可以通过几种方式运行。希望有帮助。