私は文字列 "ABBA"が存在するかどうかを調べるために、AsおよびBsの文字列を検索する有限状態マシンのモデリングのためのwhileスイッチケースを作成しようとしています。私がちょうど "ABBA"を入力すると、それは仮定されているようにWord found!
を出力します。しかし、"AABBA"
を入力すると、&という単語が正しく出力されません。どんな助けもありがとうございます。ありがとう!"ABBA"を検索する有限状態マシン
import java.util.*;
public class AB{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String word = input.next();
int current = 0;
int status = 0;
System.out.println("Starting to evaluate...");
while(status != 4){
for(int i = current; i < word.length(); i++){
String part = word.substring(current, i + 1);
switch(status){
case 0: //start
if(part.equals("A")){
status = 1;
}
else if(part.equals("B"){
status = 0;
current = i;
}
break;
case 1: //A is there now
if(part.equals("AB")){
status = 2;
}
else if(part.equals("AA"){
status = 1;
current = 1;
}
break;
case 2: //AB is there now
if(part.equals("ABB")){
status = 3;
}
else if(part.equals("ABA"){
status = 1;
current = 1;
}
break;
case 3: //ABB is there now
if(part.equals("ABBA")){
status = 4;
System.out.println("Word found!");
}
else if(part.equals("ABBB"){
status = 0;
current = i;
}
break;
}
}
}
}
}
中/スイッチ構造が要件ですか? –
@AlexeyR。そうです – Anna
私はString word = input.next()をString word = "AABBA"に変更しました。あなたはそれがスキャナの問題ではないと確信していますか? – Malphrush