程序一直卡在for循环不动了怎么办
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;
public class outPutList {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String question = sc.nextLine();
int numOfVariables;
ArrayList<String > variables = new ArrayList<String>();
int startOfQuestion = question.indexOf('=') + 1;
int nowPosition = startOfQuestion;
char nowChar;
String contentOfQuestion = question.substring(startOfQuestion);
//找出各个变量名称及题目中变量的数量
while(notEnd(question,nowPosition)){
nowChar = question.charAt(nowPosition);
System.out.println("现在检查"+nowChar);
if (nowChar == 'p' || nowChar == 'q' || nowChar == 'r' || nowChar == 's'){
if (notContain(variables,nowChar)){
variables.add(String.valueOf(nowChar));
System.out.println("发现变量"+nowChar);
}
}
nowPosition++;
System.out.println("现在位置为"+nowPosition);
}
numOfVariables = variables.size();
System.out.println("共发现"+numOfVariables+"个变量");
variables.add("A");
//写出真值表的表头
String[][] answerList = new String[numOfVariables + 1][(int)Math.pow(2,numOfVariables) + 1];
System.out.println("已创建answerList,大小为"+answerList.length+","+answerList[0].length);
for (int x = 0;x < numOfVariables+1;x++){
answerList[x][0] = (String) variables.get(x);
System.out.println("获取了"+(String) variables.get(x)+"并将其填入answerList");
}
//向表中填入变量取值
int needToInsert0;
int needToInsert1;
int numOfNeedToInsert;
System.out.println("开始填入真值");
for (int x = 0;x < numOfVariables;x++){
numOfNeedToInsert = (int)Math.pow(2,x);
needToInsert0 = numOfNeedToInsert;
needToInsert1 = numOfNeedToInsert;
for (int y = 1;y <= (int)Math.pow(2,numOfVariables);y++){
if (needToInsert0 == 0 && needToInsert1 == 1){
needToInsert0 = numOfNeedToInsert;
needToInsert1 = numOfNeedToInsert;
}else if (needToInsert1 != 0 && needToInsert0 != 0){
answerList[x][y] = String.valueOf(1);
}else if (needToInsert1 == 0 && needToInsert0 != 0){
answerList[x][y] = String.valueOf(0);
}
}
}
//计算
int nowRows = 1;
for (int y = 1;y <= (int)Math.pow(2,numOfVariables);y++){
String[][] list = new String[numOfVariables][2];
System.out.println("第"+y+"次创建用于计算的真值表");
for (int x2 = 0;x2 < list.length;x2++){
list[x2][0] = answerList[x2][0];
System.out.println("填入1");
list[x2][1] = answerList[x2][nowRows];
System.out.println("填入2");
}
answerList[numOfVariables][y] = calculate(list,contentOfQuestion);
System.out.println("将"+answerList[numOfVariables][y]+"填入answerList表中的"+numOfVariables+"列"+y+"行");
nowRows++;
}
int count = 1;
System.out.println("开始输出最终真值表");
for (String[] x:answerList) {
if (count == answerList.length){
System.out.println(x);
count = 1;
}else {
System.out.print(x + " ");
count++;
}
}
}
public static boolean notEnd(String sentence,int now){
if (sentence.length()-1 != now){
return true;
}else{
return false;
}
}
public static boolean notContain(ArrayList arrayList,char c){
for (int x = 0;x < arrayList.size();x++){
if (arrayList.get(x).equals(String.valueOf(c))){
return false;
}
}
return true;
}
public static String calculate(String[][] list,String question){
question = "(" + question + ")";
for (int x = 0;x < list.length;x++){
question.replace(list[x][0],list[x][1]);
}
change1To0(question);
while (question != "1" ||question != "0"){
simplify(question);
}
return question;
}
public static String simplify(String question){
int indexOfStart = 0;
int indexOfEnd = question.length();
String part;
for (int x = 0;x < question.length();x++){
if (question.charAt(x) == '('){
indexOfStart = x;
}
if (question.charAt(x) == ')'){
indexOfEnd = x;
break;
}
}
part = question.substring(indexOfStart+1,indexOfEnd-1);
while (true){
if (part.substring(0,2).equals("1∧1")
||part.substring(0,2).equals("1∨1")
||part.substring(0,2).equals("1∨0")
||part.substring(0,2).equals("0∨1")){
if (part.length() <= 3){
part = "1";
break;
}else {
part = "1" + part.substring(3);
System.out.println("输出"+part);
}
}else {
if (part.length() <= 3){
part = "0";
break;
}else {
part = "0" + part.substring(3);
System.out.println("输出"+part);
}
}
}
if (indexOfStart != 0 && indexOfEnd != question.length()){
return question = question.substring(0,indexOfStart-1) + part + question.substring(indexOfEnd+1);
}else if (indexOfStart == 0 && indexOfEnd == question.length()){
return question = part;
}else if (indexOfEnd == question.length() && question.charAt(indexOfStart) == '﹁' && question.length() == 4){
if (question.charAt(3) == '1'){
return question = "0";
}else {
return question = "1";
}
}else {
return question = "0";
}
}
public static String change1To0(String question){
for(int x = 0;x < question.length();x++){
if (question.charAt(x) == '﹁'){
if (question.charAt(x+1) != '('){
if (question.charAt(x+1) == '1'){
question = question.substring(0,x-1) + '0' + question.substring(x+2);
}else {
question = question.substring(0,x-1) + '1' + question.substring(x+2);
}
}
}
}
return question;
}
}
一个命题真值表计算器
哪个for循环。
加点输出看下变量值情况吧,或者debug调试。
卡在
int nowRows = 1;
for (int y = 1;y <= (int)Math.pow(2,numOfVariables);y++){
String[][] list = new String[numOfVariables][2];
System.out.println("第"+y+"次创建用于计算的真值表");
for (int x2 = 0;x2 < list.length;x2++){
list[x2][0] = answerList[x2][0];
System.out.println("填入1");
list[x2][1] = answerList[x2][nowRows];
System.out.println("填入2");
}
这一段循环结束后不动了