配置环境
配置JDK
- 我选择在Windows中配置
! - 按照教程,进行环境变量的配置。
- 因为我将安装的jdk放在
D-jdk1.8-java
中,所以按如下方式配置编辑path
变量。
- 此时jdk安装和配置完成!
下载IDE
克隆项目
- 先进行注册,用户名设置为学号
20165221
,邮箱为自己的QQ邮箱876812128@qq.com
注册成功界面如下
- 然后进行登录
- 查看登录信息
- 在
git
命令行找到操作地址
- 阿超的代码
import java.util.Stack;
public class Main {
private static String[] op = { "+", "-", "*", "/" };// Operation set
public static void main(String[] args) {
String question = MakeFormula();
System.out.println(question);
String ret = Solve(question);
System.out.println(ret);
}
public static String MakeFormula(){
StringBuilder build = new StringBuilder();
int count = (int) (Math.random() * 2) + 1; // generate random count
int start = 0;
int number1 = (int) (Math.random() * 99) + 1;
build.append(number1);
while (start <= count){
int operation = (int) (Math.random() * 3); // generate operator
int number2 = (int) (Math.random() * 99) + 1;
build.append(op[operation]).append(number2);
start ++;
}
return build.toString();
}
public static String Solve(String formula){
Stack<String> tempStack = new Stack<>();//Store number or operator
Stack<Character> operatorStack = new Stack<>();//Store operator
int len = formula.length();
int k = 0;
for(int j = -1; j < len - 1; j++){
char formulaChar = formula.charAt(j + 1);
if(j == len - 2 || formulaChar == '+' || formulaChar == '-' || formulaChar == '/' || formulaChar == '*') {
if (j == len - 2) {
tempStack.push(formula.substring(k));
}
else {
if(k < j){
tempStack.push(formula.substring(k, j + 1));
}
if(operatorStack.empty()){
operatorStack.push(formulaChar); //if operatorStack is empty, store it
}else{
char stackChar = operatorStack.peek();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')){
operatorStack.push(formulaChar);
}else {
tempStack.push(operatorStack.pop().toString());
operatorStack.push(formulaChar);
}
}
}
k = j + 2;
}
}
while (!operatorStack.empty()){ // Append remaining operators
tempStack.push(operatorStack.pop().toString());
}
Stack<String> calcStack = new Stack<>();
for(String peekChar : tempStack){ // Reverse traversing of stack
if(!peekChar.equals("+") && !peekChar.equals("-") && !peekChar.equals("/") && !peekChar.equals("*")) {
calcStack.push(peekChar); // Push number to stack
}else{
int a1 = 0;
int b1 = 0;
if(!calcStack.empty()){
b1 = Integer.parseInt(calcStack.pop());
}
if(!calcStack.empty()){
a1 = Integer.parseInt(calcStack.pop());
}
switch (peekChar) {
case "+":
calcStack.push(String.valueOf(a1 + b1));
break;
case "-":
calcStack.push(String.valueOf(a1 - b1));
break;
case "*":
calcStack.push(String.valueOf(a1 * b1));
break;
default:
calcStack.push(String.valueOf(a1 / b1));
break;
}
}
}
return formula + "=" + calcStack.pop();
}
}
- 以Github账号命名的文件夹作为项目目录,在 IntelliJ IDEA中新建一个工程。初步运行截图如下
- 新建测试方法
单元测试
- 单步运行,第34行设置断点,观察程序运行
- 条件断点,令
j==0
,操作如下
总结
- 感觉和之前的实验一相似,但是这个的提交代码的方式更加便捷。
- 很多都在idea的测试中都学习过了
建议
- 对于第六部分的效能工具没有叙述完整,可以与我们原先做的实验做比较,加深理解。
- 教程步骤很清晰,但步骤有些过简,比如单步执行给出了两个操作指令
step over
与step into
,并没有详细讲二者间的差别。