以03/02的形式识别日期:2月3日,为了简单起见,我们认为每个月由30天组成

问题描述 投票:0回答:1

这是代码:

package Tp2;

import java.util.Date;
import java.util.Scanner;

public class EX2_Date_forme {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("give a string or (exit) to leave : ");
            String input = sc.nextLine();

            if (input.equals("exit")) {
                break;
            } else {
                System.out.println();
                Date(input);
            }
        }
    }

    public static void Date(String input) {
        int len = input.length();
        int state = 0;
        int tracker = 0;
        for (int i = 0; i < len; i++) {
            char c = input.charAt(i);
            boolean c_isBetween_zero_in_AndNine = Character.getNumericValue(c) >= 0 && Character.getNumericValue(c) <= 9;
            boolean c_isBetween_zero_out_AndNine = Character.getNumericValue(c) > 0 && Character.getNumericValue(c) <= 9;
            switch (state) {

                case 0:
                    if (c == '0'){
                        state = 1;
                        tracker++;
                    }
                    else if (c == '1' || c == '2'){
                        state = 2;
                        tracker++;
                    }
                    else if (c == '3'){
                        state = 3;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 1:
                    if (c_isBetween_zero_out_AndNine) {
                        state = 4;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 2:
                    if (c_isBetween_zero_in_AndNine){
                        state = 4;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 3:
                    if (c == '0') {
                        state = 4;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 4:
                    if (c == '/') {
                        state = 5;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 5:
                    if (c == '0'){
                        state = 6;
                        tracker++;
                    }
                    else if (c == '1'){
                        state = 7;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 6:
                    if (c_isBetween_zero_out_AndNine){
                        state = 8;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 7:
                    if (c == '0' || c == '1' || c == '2'){
                        tracker++;
                        state = 8;
                    }
                    else System.out.println("error");
                    break;
                case 8:
                    if (tracker == len) System.out.println("in the language");
                    else
                        System.out.println("error");
                    break;
            }
        }
    }
}

我们想要识别以下形式的日期: 03/02 为 2 月 3 日,仅限当年。

为了简单起见,我们认为每个月都是由组成的 30 天。

例如,当我在调试模式下输入“01/01”时,一切都会按预期工作,直到最后一次迭代,其中 i=4、len=5 和 state=8。然而for循环并没有继续进入case 8。

我期望看到“在语言消息中”,但我没有得到结果

当我在循环后添加它时它起作用了并且它起作用了:

    if (state == 8 && tracker == len) {
        System.out.println("in the language");
    } else {
        System.out.println("error");
    }

但我希望它输入案件编号8

java for-loop compilation switch-statement automaton
1个回答
0
投票

方法 Date 中的代码没有进入情况 8,因为,到 'state' 时 达到 8,for 循环结束。
为了解决这个问题,我调整了 参数以确保额外的迭代,从而允许代码 再执行一次重复。然而,在最后一次迭代中,当 'i' 超过了输入的长度,我在外部定义了变量 for 循环并添加了一个“if”语句,仅当“i”为时才赋值 小于长度。

这是经过必要更改的代码:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    while (true) {
        System.out.println("give a string or (exit) to leave : ");
        String input = sc.nextLine();

        if (input.equals("exit")) {
            break;
        } else {
            System.out.println();
            Date(input);
        }
    }
}

public static void Date(String input) {
    int len = input.length();
    int state = 0;
    int tracker = 0;
    char c = ' ';
    boolean c_isBetween_zero_in_AndNine = false;
    boolean c_isBetween_zero_out_AndNine = false;
    for (int i = 0; i <= len; i++) {
        if (i < len) {
            c = input.charAt(i);
            c_isBetween_zero_in_AndNine = Character.getNumericValue(c) >= 0 && Character.getNumericValue(c) <= 9;
            c_isBetween_zero_out_AndNine = Character.getNumericValue(c) > 0 && Character.getNumericValue(c) <= 9;
        }
        switch (state) {

            case 0:
                if (c == '0'){
                    state = 1;
                    tracker++;
                }
                else if (c == '1' || c == '2'){
                    state = 2;
                    tracker++;
                }
                else if (c == '3'){
                    state = 3;
                    tracker++;
                }
                else System.out.println("error");
                break;
            case 1:
                if (c_isBetween_zero_out_AndNine) {
                    state = 4;
                    tracker++;
                }
                else System.out.println("error");
                break;
            case 2:
                if (c_isBetween_zero_in_AndNine){
                    state = 4;
                    tracker++;
                }
                else System.out.println("error");
                break;
            case 3:
                if (c == '0') {
                    state = 4;
                    tracker++;
                }
                else System.out.println("error");
                break;
            case 4:
                if (c == '/') {
                    state = 5;
                    tracker++;
                }
                else System.out.println("error");
                break;
            case 5:
                if (c == '0'){
                    state = 6;
                    tracker++;
                }
                else if (c == '1'){
                    state = 7;
                    tracker++;
                }
                else System.out.println("error");
                break;
            case 6:
                if (c_isBetween_zero_out_AndNine){
                    state = 8;
                    tracker++;
                }
                else System.out.println("error");
                break;
            case 7:
                if (c == '0' || c == '1' || c == '2'){
                    tracker++;
                    state = 8;
                    continue;
                }
                else System.out.println("error");
                break;
            case 8:
                if (tracker == len) System.out.println("in the language");
                else
                    System.out.println("error");
                break;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.