Javaを使用し、入力値の一部を一致させ、法律に従って出力します

ここに画像の説明を挿入

package Interview;

import com.sun.javafx.image.BytePixelSetter;
import jdk.internal.util.xml.impl.Input;
import jdk.nashorn.internal.ir.CaseNode;

import java.util.Scanner;

/**
 * @author 关根普
 * @create 2021-02-19 10:28
 */
public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入8位设备编号");
        String code = scanner.next();
        Rule rule = new Rule();
        System.out.println(rule.a(code));
        scanner.close();
    }
}
class Rule{
    
    
    public String a(String code){
    
    
        String b ="";
        String c ="";

        for(int i =0;i<4;i++){
    
    
            b+=code.charAt(i);
        }

        switch (b){
    
    
            case "3251":
                System.out.println("南京市");
                break;
            case "3252":
                System.out.println("无锡市");
                break;
            case "3253":
                System.out.println("徐州市");
                break;
            case "3254":
                System.out.println("常州市");
                break;
            case "3255": for(int i =4;i<6;i++){
    
    
                b+=code.charAt(i);}
                if( b.equals("325508")){
    
    
                    System.out.println("昆山市");
                    break;}
                System.out.println("苏州市(不含昆山)");
                break;
            case "3256":
                System.out.println("南通市");
                break;
            case "3257":
                System.out.println("连云港");
                break;
            case "3258":
                System.out.println("淮安市");
                break;
            case "3259":
                System.out.println("盐城市");
                break;
            case "3260":
                System.out.println("扬州市");
                break;
            case "3261":
                System.out.println("镇江市");
                break;
            case "3262":for(int i =4;i<6;i++){
    
    
                b+=code.charAt(i);}
                if( b.equals("326205")){
    
    
                    System.out.println("泰兴市");
                    break;}
                System.out.println("泰州市(不含泰兴)");
                break;
            case "3263":for(int i =4;i<6;i++){
    
    
                b+=code.charAt(i);}
                if( b.equals("326303")){
    
    
                    System.out.println("沐阳市");
                    break;}
                System.out.println("宿迁市(不含沐阳)");
                break;

            default:
                System.out.println("输入编号有误,请重新输入");

        }
        return(c);}

}

発生した問題:入力部分の長さを正確に8に設定して、「入力番号が標準化されていない」が不十分または超過した場合に出力されるようにするにはどうすればよいですか?

おすすめ

転載: blog.csdn.net/qq_43021902/article/details/113922481