if(currentLoopIndex == rules.length-1){ shiftName = rules[0]; shiftName2 = rules[1]; }else if(currentLoopIndex + 1 == rules.length-1){ shiftName = rules[currentLoopIndex+1]; shiftName2 = rules[0]; }else if(currentLoopIndex + 2 == rules.length-1){ shiftName = rules[currentLoopIndex+1]; shiftName2 = rules[currentLoopIndex+2]; } 优化这段代码
时间: 2024-03-17 15:42:29 浏览: 16
可以使用取模运算符 `%` 来简化这段代码。具体实现如下:
```
shiftName = rules[(currentLoopIndex + 1) % rules.length];
shiftName2 = rules[(currentLoopIndex + 2) % rules.length];
```
这样,无论 `currentLoopIndex` 的值是多少,都可以通过取模运算来实现对 `rules` 数组的循环访问。这样代码更加简洁,也更易于理解。
相关问题
public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num=scan.nextInt(); String[] rules=new String[num]; for(int i=0;i<num;i++) rules[i]=scan.next(); String strin = scan.next(); ArrayList<String> product = new ArrayList<String>(); for(int i=0; i<rules.length; i++) { char[] reg = rules[i].toCharArray(); for(int j=0; j<reg.length; j++) { if(reg[j] >= 'A' && reg[j]<= 'Z'); else { if(!product.contains(reg[j]+"")) product.add(reg[j]+""); } } } Main m = new Main(); ArrayList<String> rule = m.getMostLeft(rules, strin); rule.add(0,rules[0]); String left = new String(); for(int i=0; i<rule.size()-1; i++) { int flag = 0; String right = new String(); if(i == 0) { System.out.print(rules[0].charAt(0)+"=>"); for(int j=3; j<rules[0].length(); j++) left += rules[0].charAt(j); System.out.print(left); } System.out.print("=>"); for(int j = 0; j < left.length(); j++) { if(left.charAt(j) == rule.get(i+1).charAt(0) && flag == 0){ if(rule.get(i+1).charAt(3) == '@') { flag = 1; continue; } for(int z = 3; z < rule.get(i+1).length(); z++) right += rule.get(i+1).charAt(z); flag = 1; } else right += left.charAt(j); } left = right; System.out.print(right); } }解释一下
这是一个Java程序的main方法,它接受用户输入的一些规则和一个字符串,然后根据这些规则将字符串推导到最左侧。具体来说,程序首先从控制台读取一个整数num,表示规则的数量,然后读取num个字符串,表示所有的规则。接着,程序读取一个字符串strin,表示要进行推导的输入串。
程序接下来的操作是将所有规则中出现的终结符号收集起来,存储在一个ArrayList<String>对象product中。然后程序调用Main类的getMostLeft方法,找到最左侧的推导规则,并存储在一个ArrayList<String>对象rule中。最后,程序根据规则和输入串进行推导,将推导过程输出到控制台上。
具体来说,程序首先将起始符号输出,然后将起始符号的产生式中的非终结符号保存到left变量中。接着,程序逐步推导输入串,每次将推导结果输出到控制台上。在推导过程中,程序遍历left中的每个字符,如果遇到了一个与当前推导规则匹配的非终结符号,就将其展开为相应的产生式,并将结果保存到right变量中。最后将left变量更新为right变量,继续下一轮推导过程。程序输出的结果是一个完整的推导序列,从起始符号到最终推导结果。
解释代码<template> <div class="paragraph-info"> <!-- 显示段落内容 --> <div class="header"> <el-button text type="danger" @click="state.clearAll">一键清空</el-button> <span>共计:</span> <b>{{ state.data.length }}</b> </div> <div class="box"> <template v-if="state.data.length > 0"> <div class="item" v-for="(i, k) in state.data" :key="k"> <div class="tp"> <div class="tt"> {{ i.title }} </div> <div class="act"> <el-button size="small" type="primary" text @click="state.open(i)" >编辑</el-button > <el-button size="small" type="danger" text @click="state.del(i)" >删除</el-button > </div> </div> <div class="cnt"> <div v-html="i.content"></div> </div> </div> </template> <template v-else> <div class="no-data">点击下方“新增段落”按钮新增。</div> </template> </div> <div class="plus-btn" @click="state.open()">新增段落</div> </div> <!-- 新增窗口 --> <el-dialog v-model="state.dialog" append-to-body :close-on-click-modal="false" destroy-on-close width="996px" top="5%" > <template #header> <slot>信息更新</slot> </template> <el-card> <el-form :model="state.form" ref="formRef" label-position="top" :rules="state.rules" > <el-form-item label="标题" prop="title"> <el-input placeholder="请输入标题" v-model.number="state.form.title" ></el-input> </el-form-item> <el-form-item label="内容" prop="content"> <self-editor v-model="state.form.content"></self-editor> </el-form-item> </el-form> </el-card> <template #footer> <el-button type="primary" @click="state.handler()"> 保存 </el-button> <el-button @click="state.dialog = false">关闭</el-button> </template> </el-dialog> </template>
这段代码是一个Vue组件的模板代码,包含了一个用于显示段落信息的页面和一个用于新增或编辑段落的弹出窗口。具体解释如下:
1. 页面部分:
- 使用了一个名为paragraph-info的div作为组件的根元素;
- 在header中添加了一个清空按钮和一个显示段落数量的元素;
- 在box中使用了v-if和v-for指令来循环渲染每个段落的标题和内容,当段落数量为0时,显示一个“点击下方‘新增段落’按钮新增”提示;
- 在plus-btn中添加了一个用于打开弹出窗口的按钮。
2. 弹出窗口部分:
- 使用了一个el-dialog组件作为弹出窗口,绑定了一个dialog变量用于控制其显示和隐藏;
- 在header slot中添加了一个“信息更新”的标题;
- 在el-form中添加了两个el-form-item元素,用于输入段落标题和内容;
- 在el-form-item中使用了prop属性来指定字段名,使用了v-model指令来绑定输入框的内容;
- 在最后的footer slot中添加了一个保存按钮和一个关闭按钮,点击保存按钮后会触发handler方法保存表单数据。