简体   繁体   English

我如何使循环停止在Java中重复

[英]How do i make the loop stop repeating in java

I'm a beginner in Java and I have to write a program that allows the user to enter in words, then the program returns the word backwards until the user writes "stop". 我是Java的初学者,我必须编写一个程序以允许用户输入单词,然后该程序向后返回单词,直到用户编写“ stop”为止。 Every time the user enters a word, java outputs it backwards plus the previous word which is outputted and I don't want that. 每次用户输入一个单词时,java会将其向后输出,再加上输出的前一个单词,我不希望这样。 For example, if I put input pots it outputs, stop if I print cat it outputs potstac How can I just get java to just output the words backwards without adding it on to the prior words For example, i want to input in pots it should output, stop i want to print cat it should output, tac 例如,如果我将输入的pots放置在输出中,如果我打印cat则输出potstac,请停止我如何才能让java仅向后输出单词而不将其添加到先前的单词上?例如,我想输入它输出,停止我要打印的猫,它应该输出,TAC

import java.util.*;
public class javapdf2413 {
    public static void main (String [] args) {
        Scanner in = new Scanner (System.in);

        String wordEntered = "";
        String backWords = "";

        do {
            System.out.println("Enter in a word");
            wordEntered = in.next();
            for (int i = wordEntered.length()-1; i>=0; i--) {
                backWords = backWords + wordEntered.charAt(i);
            }
            System.out.println(backWords);

        }while(!wordEntered.equals("stop"));
    }

} }

You'll need to set backWords back to an empty string at the beginning of the do loop. 您需要在do循环开始时将backWords设置回空字符串。 If you don't it will just concatenate onto the end of the previous string - which is what you said is happening. 如果您不这样做,它将仅连接到上一个字符串的末尾-这就是您所说的情况。 Setting it back to "" at the beginning of the loop body will essentially "reset" it for the next word. 在循环主体的开头将其设置回""实际上将为下一个单词“重置”它。

Like this: 像这样:

do {
    backWords = "";
    System.out.println("Enter in a word");
    wordEntered = in.next();
    for (int i = wordEntered.length()-1; i>=0; i--) {
        backWords = backWords + wordEntered.charAt(i);
    }
    System.out.println(backWords);

}while(!wordEntered.equals("stop"));

Try to do this with while loop 尝试使用while循环来做到这一点

import java.util.Scanner;

public class TestRun {

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        String wordEntered = "";
        String backWords = "";

        while (true) {
            System.out.println("Enter in a word");
            wordEntered = in.next();

            if (wordEntered.equals("stop")) {
                break;
            } else {
                for (int i = wordEntered.length() - 1; i >= 0; i--) {
                    backWords = backWords + wordEntered.charAt(i);
                }
                System.out.println(backWords);
                backWords = "" ;
            }

        }

    }

} }

Just initialize your variables inside your do loop 只需在do循环中初始化变量

String wordEntered;
String backWords;

            do {
              wordEntered = "";
              backWords = "";
                System.out.println("Enter in a word");
                wordEntered = in.next();
                for (int i = wordEntered.length()-1; i>=0; i--) {
                    backWords = backWords + wordEntered.charAt(i);
                }
                System.out.println(backWords);

            }while(!wordEntered.equals("stop"));

Just beware - Strings are immutable objects. 提防-字符串是不可变的对象。 Use it with right use case and approach. 将其与正确的用例和方法一起使用。 Try to use StringBuilder for non concurrent code as much as possible where ever you can. 尽可能尝试将StringBuilder用于非并发代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM