猿问

如何编写一种方法,将单词向后存储在从用户输入接收到的数组列表中,直到输入“完成”?

编写一个方法,该方法不断地从用户那里读取单词,直到输入的单词“完成”为止,并将每个单词按输入顺序逆向拼写的每个单词存储在 ArrayList 中。


这是我到目前为止的代码,但是每当我尝试在主类中调用它时,它都会失败。有人可以帮帮我吗?我已经包括了我在其中创建方法的类和调用它的主类。我不能组合这两个类,所以我需要能够在我的主类中从 todo 调用对象。提前致谢!


示例输入:香蕉赛车小狗完成结果数组列表:anaab Racecar yppup


import java.util.Scanner;

import java.util.ArrayList; 


public class todo extends main{



    public ArrayList <String>storeBackwards (Scanner keyboard) {

        ArrayList<String> words=new ArrayList (); 

        ArrayList<String> backwards=new ArrayList(); 


        String input=keyboard.next();

        while(!input.equalsIgnoreCase("done")) {

            words.add(input); 

            String get=""; 

            String back=""; 

            for(int i=0; i<words.size();++i) {

                get=words.get(i);

                for(i=get.length()-1; i>=0; i--) {

                    back=back+get.charAt(i); }

                backwards.add(back);

                }


            }

        return backwards;



        }

    }

这是主类


import java.util.Scanner;

public class main {


public static void main(String[] args) {

    Scanner keyboard=new Scanner(System.in); 

    todo x=new todo(); 


    x.storeBackwards(keyboard);



}


}


侃侃尔雅
浏览 214回答 2
2回答

哆啦的时光机

在您的代码中,String input=keyboard.next();您只调用了keyboard.next()一次。所以在input读取第一个标记后,String 变量的值不变。所以它导致无限循环,这就是你得到异常的原因而在两者的for循环使用的循环变量int i,使用像不同的变量int j的嵌套for循环for(int j=get.length()-1; j>=0; j--) {&nbsp; &nbsp; &nbsp; back=back+get.charAt(j);&nbsp;&nbsp;}如下修改你的代码后,你的测试用例被清除了&nbsp; &nbsp; public List<String> storeBackwards (Scanner keyboard) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> backwards=new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String input=keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(!input.equalsIgnoreCase("done")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String back="";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=input.length()-1; j>=0; j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; back=back+input.charAt(j);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; backwards.add(back);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; back="";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input=keyboard.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return backwards;&nbsp; &nbsp; }

眼眸繁星

您还可以使用 StringBuffer 的预定义 reverse() 函数,如下面的代码片段所示:ArrayList<String> backwards = new ArrayList();&nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; String input = keyboard.next();&nbsp; &nbsp; &nbsp;StringBuffer a = new StringBuffer(input);&nbsp; &nbsp; while (!input.equalsIgnoreCase("done")) {&nbsp; &nbsp; &nbsp; &nbsp; a = new StringBuffer(input);&nbsp; &nbsp; &nbsp; &nbsp; backwards.add(a.reverse().toString());&nbsp; &nbsp; &nbsp; &nbsp; input = keyboard.next();&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答