You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Switch语句中布尔逻辑失效及Do-While循环异常问题排查

控制台文本居中函数长文本拆分问题

我正在为个人项目编写实现控制台文本居中显示的函数,短文本居中问题已解决,但处理长文本时出现异常,代码运行不稳定,推测存在未知逻辑问题。我对switch语句经验较少,希望通过实践解决问题。

该函数需将输入长字符串拆分为两个子串,在200字符宽的控制台居中显示,拆分优先级依次为句号(. )、逗号(, )、空格( ),要求拆分位置在175-200字符之间,通过do-while循环依次尝试拆分规则,问题集中在splitWords(String words)函数中。

完整代码

public static void printCenter(String words){ //assumes a 200 character wide console

        int x = words.length();
        int y = (200-x);
        int z= y/2;

        if(x<175){ //no need for formatting second line
            for(int i=z; i>0; i--){
                System.out.print(" ");
            }
                System.out.print(words);
            for(int i=z; i>0; i--){
                System.out.print(" ");
            }
            System.out.println("");
        }
        if(x>=175){
            String[] split = splitWords(words);
            String words1 = split[0];
            String words2 = split[1];

            int x1 = words1.length();
            int x2 = words2.length();
            int y1 = (200-x1);
            int y2 = (200-x2);
            int z1= y1/2;
            int z2= y2/2;

            System.out.println("Split at: " + x1 + "	 Output below");
            System.out.println("");

            for(int i=z1; i>0; i--){
                System.out.print(" ");
            }
                System.out.print(words1);
            for(int i=z1; i>0; i--){
                System.out.print(" ");
            }
            for(int i=z2; i>0; i--){
                System.out.print(" ");
            }
                System.out.print(words2);
            for(int i=z2; i>0; i--){
                System.out.print(" ");
            }
            System.out.println("");
        }

    }
public static String[] splitWords(String words){
    String line1 = "";
    String line2 = "";
    int splithere = 0;
    //strings used to create new substrings
    String firstpref = ". ";
    String secondpref = ", ";
    String lastpref = " ";
    int tries = 0;
    boolean set = false;
    System.out.println("started");
    do{
        switch(tries){
            case 0:
                int num = words.indexOf(firstpref,175);
                if(num<200){
                    System.out.print("this should be less than 200: " + num);
                    splithere = words.indexOf(firstpref,175);
                    set=true;
                    System.out.println("found valid split during pass: " + tries + " at: " + splithere);
                }
            case 1:
                if(words.indexOf(secondpref,175)<200){
                    splithere = words.indexOf(firstpref,175);
                    set=true;
                    System.out.println("found valid split during pass: " + tries + " at: " + splithere);
                }
            case 2:
                if(words.indexOf(lastpref,175)<200){
                    splithere = words.indexOf(firstpref,175);
                    set=true;
                    System.out.println("found valid split during pass: " + tries + " at: " + splithere);
                }
        }
        tries++;
    }while(set = false);

    line1 = words.substring(0,splithere);
    line2 = words.substring(splithere,words.length());
    String[] output = {line1,line2};
    System.out.println(" ended with boolean = " + set);
    return output;
    }

347字符字符串的测试输出

started
found valid split during pass: 0 at: 235
ended with boolean = false
Split at: 235 Output below:

You are at a junction in the middle of town. To the west, through a guard post, lies the heart of the village, protected and highly guarded this area is home to politicians, great scribes and hunters and most importantly, cheif's perch (String splits here). The older dirt road running North ends in a T with a decorated stone road running East.

问题现象

从输出可见两个核心问题:

  • switch内的if布尔逻辑未生效:当拆分位置为235(超出200字符限制)时,仍打印了验证通过的日志;
  • do-while循环未按预期执行:在set=false时直接终止,没有递增tries并再次尝试switch中的拆分规则。

内容的提问来源于stack exchange,提问作者Chance H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 14:47:45