java - 字符串仅等于字符串的一部分

标签 java arrays foreach

我正在编写一个代码,根据用户输入(a和b)创建一个新单词(c),所以我需要一个代码来检查一个字符串是否等于另一个字符串,但它显示该字符串仅等于a另一个字符串的一部分。

import java.util.Scanner;

public class GabungKata_1402019129 {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("----------------------------------");
    System.out.println("    Program Gabung Kata");
    System.out.println("    Dibuat oleh 1402019129");
    System.out.println("----------------------------------");       

    System.out.print("Masukkan kata pertama: "); // Enter First word
    String first = sc.nextLine();                // Bbxx
    System.out.print("Masukkan kata kedua  : "); // Enter Second word
    String second = sc.nextLine();               // oxxx

    String result = ""; // new String that is a combination from first and 
                        // second                           
    String[] names = {"Bob", "Zidan", "Fawzan", "Arkan", "Raihan"};
    boolean data = true;
    int n = 0;

    do {


        for (String check: names) {      

            if (result.equals(check)) {
                data = false;
            }else
                data = true;
        }

        result += first.charAt(n);
        result += second.charAt(n);
        n++;            

    } while(data & n < first.length() & n < second.length());

    System.out.println("New Word: " + result);
    System.out.println("Is the new word is one of the names? " + !data);

 }
}

新单词将显示:“Bobxxxxx”,但我认为当结果等于“Bob”时应该停止。我需要的是一段代码,用于在结果等于其中一个名称时停止循环。我英语不好所以希望你们能理解。

最佳答案

根据您的描述,这应该可行,但我确信有更好的方法可以做到这一点:

class Scratch {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("----------------------------------");
        System.out.println("    Program Gabung Kata");
        System.out.println("    Dibuat oleh 1402019129");
        System.out.println("----------------------------------");

        System.out.print("Enter First word: "); // Enter First word
        String firstString = sc.nextLine();                // Bbxx
        System.out.print("Enter Second word  : "); // Enter Second word
        String secondString = sc.nextLine();               // oxxx

        String combinedString = ""; // new String that is a combination from first and
        // second
        String[] targetNames = {"Bob", "Zidan", "Fawzan", "Arkan", "Raihan"};
        boolean shouldContinue = true;
        int index = 0;

        String interlacedString = createInterlacedString(firstString, secondString);
        do {
            for (String targetName: targetNames) {

                if (combinedString.equals(targetName)) {
                    shouldContinue = false;
                    break;
                }
            }

            if(shouldContinue){
                combinedString += interlacedString.charAt(index);
                index++;
            }
        } while(shouldContinue
                && (index < firstString.length())
                && (index < secondString.length())
        );

        System.out.println("New Word: " + combinedString);
        System.out.println("Is the new word is one of the names? " + !shouldContinue);

    }

    public static String createInterlacedString(String stringOne, String stringTwo){
        String interlacedString = "";
        for(int i = 0; i < stringOne.length(); i++){
            interlacedString += stringOne.charAt(i);
            if(i < stringTwo.length()){
                interlacedString += stringTwo.charAt(i);
            }
        }
        if(stringTwo.length() > stringOne.length()){
            interlacedString += stringTwo.substring(stringOne.length());
        }
        return interlacedString;
    }
}

请注意,createInterlacedString 的静态类只是为了让我可以在 IDE 的临时文件中运行它。

关于java - 字符串仅等于字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58899221/

相关文章:

java - 如何实例化 EntityManager(无法构建 Hibernate SessionFactory)

arrays - React 渲染组件数组

c - 搜索数组中的重复值

php - foreach 循环和标准类对象

java - 是否可以通过编程方式创建 Freemarker 宏?

java - 使用 JGit 提交之间更改的文件列表

c# - Parallel.ForEach 对于 DataTable 来说工作太慢

c# - 一次显示 List 中的项目 10 个字符串项目

java - 有没有办法在 Android 设备上同时使用两个外部音量键来获得第三个独特的输出?

javascript - 如何在 JavaScript 中创建多维数组?