最近学校一直在教java,老觉得别的IDE用的各种不爽,还是想用自己的Sublime,在网上查找了一些方法,发现都是在sublime调试输出的,这样的缺点就是无法接受用户输入,所以我稍作改变,调用一个cmd出来解决这个问题。
1. 首先需要配置java的path环境变量
这个很好说,环境变量里配置就好了,如果不会自行百度。
2. 打开jdk的bin目录,新建runJava.bat,内容如下:
@ECHO OFF
cd %~dp1
ECHO Compiling %~nx1.......
IF EXIST %~n1.class (
DEL %~n1.class
)
javac %~nx1
IF EXIST %~n1.class (
ECHO -----------OUTPUT-----------
start cmd /k "cd %~dp1&java %~n1"
)
3. 回到Sublime,打开菜单Preference,选择Browse Packages,找到java文件夹下的JavaC.sublime-build文件,打开编辑。
{
"cmd": ["runJava.bat", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java",
"encoding": "cp936"
}
4. 然后我们随便写一个需要用户输入的java程序,比如
import java.util.Scanner;
public class Document2{
public static void main ( String[] args ){
int number = 0;
String week = "";
Scanner input = new Scanner (System.in);
System.out.print("Enter the 1-7:");
while(input.hasNext()){
number = input.nextInt();
switch (number){
case 1:
week = "Monday";
break;
case 2:
week = "Tuesday";
break;
case 3:
week = "Wednesday";
break;
case 4:
week = "Thursday";
break;
case 5:
week = "Friday";
break;
case 6:
week = "Saturday";
break;
case 7:
week = "Sunday";
break;
default:
week = "Invalid number";
break;
}
System.out.println(week);
}
}
}