【问题标题】:String Output Alignment in JavaJava中的字符串输出对齐
【发布时间】:2016-09-22 14:27:51
【问题描述】:

practice question part 1

practice question part 2

这是一道练习题,对我来说比较难。下面是我的静态方法代码(主要方法是固定不变的,并且给出了静态方法的签名),我的目的是获取字符之间的匹配并将它们打印出来。

但也有一些顾虑:

1) 我如何确保当所有字符串都对齐但有多余的字符使布尔值为假并且结果不对齐时不打印? (例如 amgk 作为第二个字符串和第一个字符串是 Java 编程课程)

2) 我如何让它打印正确?目前空格已关闭,字母不是想要的。

3) 如果str1中有多个字符a,我选择放哪个,已经有匹配的怎么省略其余的?

非常感谢能帮助指导像我这样的初学者解决这个问题的伪代码。

public class Q3 {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the first string:");
    String input1 = sc.nextLine();
    System.out.print("Enter the second string:");
    String input2 = sc.nextLine();
    System.out.println();

    if (matchStrings(input1, input2)) {
        System.out.println();
        System.out.println("There is an alignment as shown above.");
    } else {
        System.out.println("No alignment can be found.");
    }
}
    public static boolean matchStrings(String str1, String str2) {
    // Modify the code below to return the correct value.
    boolean isMatch = false;
    //int firstChar = str2.charAt(0);
    //int lastChar = str2.charAt(str2.length()-1);
    int prevIndex = 0;

    System.out.println(str1);

    for (int j = 0; j< str2.length(); j++) {
        for (int i = 0; i<str1.length();i++) {
            char charToSearch = str1.charAt(i);
            int newIndex = i;

            if (str2.charAt(j)== charToSearch) {
                for (int k = prevIndex; k < newIndex-1; k++) {
                    System.out.print(" ");
                }
                System.out.print(charToSearch);
                //prevIndex=newIndex+1;


                isMatch = true;
            }
        }
    }
    return isMatch;
}
}

【问题讨论】:

  • 从更简单的任务开始
  • 如果您需要帮助,您应该在问题中而不是在链接中创建向我们展示您的期望和实际得到的东西。请阅读How do I ask a good question?
  • 我认为您没有理解说明。要“对齐”,字符串 1 必须包含字符串 2 中的所有字母,并且顺序相同。也就是说,如果找到第一个字母,那么第二个字母应该在它之后的某个地方,第三个字母应该在它之后的某个地方,等等。 - 所以当字符串 2 包含一个不在字符串 1 中的字母时,就没有“匹配”。

标签: java string loops for-loop indexing


【解决方案1】:

我认为您在数据结构课程中学习的前几个结构中的两个是堆栈和队列。因此,我将提供一个使用堆栈的实现。您可以使用堆栈来存储测试字符串和pop 当每个字符元素与第一个字符串中的字符匹配时从堆栈中取出。否则你会在matched字符串对象中输出一个空格" "

    Stack s2 = new Stack();
    String str1 = "Java Programming";
    String str2 = "amg";

    for(int i = str2.length()-1; i >= 0; i--){ //Need to populate the stack backwards...LIFO
        s2.push(str2.charAt(i));
    }

    String match = ""; //Used to store the matching line

    for(int i = 0; i < str1.length(); i++){
        if(str1.charAt(i) == (char)s2.peek()){
            match += s2.pop().toString();
        }
        else
        {
            match += " ";
        }
    }

    System.out.println(str1);
    System.out.println(match);

您也可以为此使用队列,但我会留给您自己学习。还要练习使用数组和整数指针创建自己的 Stack 对象来处理溢出/下溢。

上面的代码会打印出来:

【讨论】:

  • 感谢您的回答!我正在上一门介绍课程,我们还没有学习推送和流行音乐。我仍然想知道如何使用简单的 for 循环和其他 java api 方法来做到这一点
猜你喜欢
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多