代码如下:
import java.io.IOException;
import java.util.Scanner;
class TransferDemo
{
public static void main(String[] args) throws IOException //抛出IOException异常
{
Scanner sc=new Scanner(System.in);//从键盘接收数据
//while循环语句
while(true)
{
System.out.print("请输入:");
int i=sc.nextInt(); //定义输入数据类型,nextInt方式接收数据
if(i==1) //当输入为1,则调用运行windows记事本
{
System.out.println("打开记事本");
Process p=Runtime.getRuntime().exec("notepad.exe");
}
else if(i==2) //当输入为2,则调用运行windows画图
{
System.out.println("打开画图程序");
Process p=Runtime.getRuntime().exec("mspaint.exe");
}
else if(i==3) //当输入为3,则调用运行windows计算器
{
System.out.println("打开计算器");
Process p=Runtime.getRuntime().exec("calc.exe");
}
else //当输入不为1.2.3,则退出程序
{
System.out.println("退出程序!");
System.exit(0);
}
}
}
}
结果如下: