如何在不退出而导致打印输出的情况下停止while循环而执行此操作?

编程器82

我尝试使用此代码,但是每次运行并键入quit时,它都会打印出结果,就好像它应该是一个单词而不是结束循环的触发器,然后循环结束。我只是尝试在if if语句中放入一个单词if quit,它将结束整个程序,但是我无法摆脱单词不等于“ quit”的do while循环。这是代码。

do
        {
            System.out.println("Enter a word and I will see if, when the first letter is made the last" + '\n' + "and this word is spelled backwards, is the original word. Type quit to end: ");
            word = keyboard.next().toLowerCase();
            
            String newWord = word.substring(1) + word.charAt(0);
            String reverse = "";
        
            for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
            {
                reverse = reverse + newWord.charAt(wordLength);
            }
        
            if (word.equals(reverse)) {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
            } else {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
            }
        }while (!word.equals("quit"));

一如既往,感谢您的帮助!

里斯

do while循环仅在到达循环末尾时检查条件。因此,代码中发生的事情是,当将word设置为“退出”时,它下面的代码仍将运行直到到达循环结束。

要实现您想做的尝试,

do
        {
            System.out.println("Enter a word and I will see if, when the first letter is made the last" + '\n' + "and this word is spelled backwards, is the original word. Type quit to end: ");
            word = keyboard.next().toLowerCase();

            if(word.equals("quit")){ break; }

            String newWord = word.substring(1) + word.charAt(0);
            String reverse = "";
        
            for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
            {
                reverse = reverse + newWord.charAt(wordLength);
            }
        
            if (word.equals(reverse)) {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
            } else {
                System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
            }
        }while (true);

这将导致for循环在输入退出时在输出文本之前停止。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在不退出终端的情况下停止bashrc执行

如何在不退出的情况下多次运行此程序?

使用sparklyr时如何在不退出RStudio的情况下停止Spark?

如何在不退出R的情况下从终端执行R一线?

如何在Rust中没有尾随换行符的情况下打印输出?

Python:在不退出代码的情况下使用相同的热键切换 while 循环

如何在不退出终端的情况下退出PostgreSQL中的select?

Emacs内部的Emacs:如何在不退出主Emacs的情况下退出内部Emacs

在不退出脚本的情况下打破循环 (bash)

如何在不停止/退出程序的情况下捕获并打印完整的异常回溯?

如何在不终止执行此操作的线程的情况下取消文件上载/下载操作?

如何在不退出脚本本身的情况下取消脚本中的命令?

如何在不退出golang程序的情况下记录错误?

如何在不退出icewm的情况下更新工作空间名称?

如何在不退出当前OS会话的情况下启动chroot?

如何在不停止循环的情况下创建在循环中的某个点触发的输出?

如何在不使用KeyboardInterrupt的情况下从按键退出while循环?[蟒蛇]

如何在不引发异常的情况下执行此操作?

在不使用Either的情况下如何在Java中执行此操作?

如何在不使用文件的情况下执行此操作?

如何在没有parens的情况下执行此操作?

如何在Shell脚本中没有“ awk”的情况下执行此操作

在没有两个查询的情况下如何在Knex中执行此操作

Java泛型,如何在没有instanceof或强制转换的情况下执行此操作?

如何在不执行功能的情况下停止打印回显结果

如何在不进入 Next 的情况下退出 For 循环

如何在不触发“打印对话框”的情况下执行SSRS URL操作?

如何在没有“ while True”循环的情况下绘制此对数?

如何在没有for循环的情况下对矩阵的每一行执行操作?