【问题标题】:Looping a program to start again after finish checking检查完成后循环程序重新开始
【发布时间】:2014-04-03 03:22:07
【问题描述】:

我正在尝试制作一个程序,用户将在 a 和 b 上输入一个字符串,该程序将检查它是否被接受。现在,如果用户输入“exit”,我设法让程序退出,但我无法弄清楚如何循环程序并在它完成检查字符串时让它重新启动,要求用户再次输入。我该怎么做?下面是我的代码

import java.util.Scanner; 
public class Automata {
public static void main(String[] args) {
    Boolean keeprunning = true;
    Scanner inputScanner = new Scanner(System.in);
    System.out.println("Please enter your input");
    String input = inputScanner.next();
    char [] Inputted = input.toCharArray();

    if (input.equalsIgnoreCase("Exit")){
        keeprunning = false;
        System.out.println("Exit now");
    }

    String stateA = "A";
    String stateB = "B";
    String stateC = "C";
    String stateD = "D";
    String stateE = "E";
    String stateF = "F";
    String currentState = "A";

    while(keeprunning){
    for (int i = 0; i < Inputted.length; i++){
        if (Inputted[i]=='a' && currentState.equals(stateA)){
            currentState = "B";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i]=='b' && currentState.equals(stateA)){
            currentState = "F";
            System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateB)){
            currentState = "B";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateB)){
            currentState = "C";
            System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateC);
        } 

        else if (Inputted[i]== 'b' && currentState.equals(stateF)){
            currentState = "F";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateF);
        } else if (Inputted[i] == 'a' && currentState.equals(stateF)){
            currentState = "B";
            System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateB);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateC)){
            currentState = "D";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateD);
        } else if (Inputted[i] == 'b' && currentState.equals(stateC)){
            currentState = "F";
            System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateF);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateD)){
            currentState = "B";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateD)){
            currentState = "E";
            System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateE);
        } 

        else if (Inputted[i] == 'a' && currentState.equals(stateE)){
            currentState = "B";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateB);
        } else if (Inputted[i] == 'b' && currentState.equals(stateE)){
            currentState = "C";
            System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateC);
        }   

    } if (currentState.equals(stateA) || currentState.equals(stateB) || currentState.equals(stateD) || currentState.equals(stateE)){
        System.out.println("Input accepted");
        keeprunning = false;
        break;
    } else if (currentState.equals(stateF) || currentState.equals(stateC)){
        System.out.println("Input not accepted");
        keeprunning = false;
        break;
    }

}
    }

感谢您的帮助:D

【问题讨论】:

    标签: java loops exit automata


    【解决方案1】:

    只需将 while 循环头移到顶部,紧跟在 keeprunning 声明之后:

    Boolean keeprunning = true;
    while(keeprunning){
    ...
    

    并从if-else 语句中删除以下代码:

    keeprunning = false;
    break;
    

    这应该可以解决你所说的问题


    import java.util.Scanner; 
    public class Automata {
    public static void main(String[] args) {
    
    Boolean keeprunning = true;
    while(keeprunning){
        Scanner inputScanner = new Scanner(System.in);
        System.out.println("Please enter your input");
        String input = inputScanner.next();
        char [] Inputted = input.toCharArray();
    
        if (input.equalsIgnoreCase("Exit")){
            keeprunning = false;
            System.out.println("Exit now");
        }
        else{
    
        String stateA = "A";
        String stateB = "B";
        String stateC = "C";
        String stateD = "D";
        String stateE = "E";
        String stateF = "F";
        String currentState = "A";
    
    
        for (int i = 0; i < Inputted.length; i++){
            if (Inputted[i]=='a' && currentState.equals(stateA)){
                currentState = "B";
                System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i]=='b' && currentState.equals(stateA)){
                currentState = "F";
                System.out.println(stateA + " -- " + Inputted[i] + " --> " + stateF);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateB)){
                currentState = "B";
                System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i] == 'b' && currentState.equals(stateB)){
                currentState = "C";
                System.out.println(stateB + " -- " + Inputted[i] + " --> " + stateC);
            } 
    
            else if (Inputted[i]== 'b' && currentState.equals(stateF)){
                currentState = "F";
                System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateF);
            } else if (Inputted[i] == 'a' && currentState.equals(stateF)){
                currentState = "B";
                System.out.println(stateF + " -- " + Inputted[i] + " --> " + stateB);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateC)){
                currentState = "D";
                System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateD);
            } else if (Inputted[i] == 'b' && currentState.equals(stateC)){
                currentState = "F";
                System.out.println(stateC + " -- " + Inputted[i] + " --> " + stateF);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateD)){
                currentState = "B";
                System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i] == 'b' && currentState.equals(stateD)){
                currentState = "E";
                System.out.println(stateD + " -- " + Inputted[i] + " --> " + stateE);
            } 
    
            else if (Inputted[i] == 'a' && currentState.equals(stateE)){
                currentState = "B";
                System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateB);
            } else if (Inputted[i] == 'b' && currentState.equals(stateE)){
                currentState = "C";
                System.out.println(stateE + " -- " + Inputted[i] + " --> " + stateC);
            }   
    
        } if (currentState.equals(stateA) || currentState.equals(stateB) || currentState.equals(stateD) || currentState.equals(stateE)){
            System.out.println("Input accepted");
    
        } else if (currentState.equals(stateF) || currentState.equals(stateC)){
            System.out.println("Input not accepted");
        }
    }   
    
    }
    }
    }
    

    【讨论】:

    • 好的,我刚刚编辑了问题。我的意思是在输出“输入接受”或“输入不接受”时循环程序,它应该在新行中显示“请输入您的输入”行并让程序再次运行,直到用户输入“退出”
    • 是的,我了解您的问题。只需按照我上面的回答中的说明进行操作即可。
    • 非常感谢。真正的欣赏
    【解决方案2】:

    对您来说最快的解决方案是在所有代码上添加一个 while(true) 循环(在 main() 的开头)

    更好的解决方案是用程序的逻辑创建一个可运行的线程,并在main()部分运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 2020-02-26
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多