【问题标题】:String equals only part of a string字符串仅等于字符串的一部分
【发布时间】:2019-11-17 09:46:18
【问题描述】:

我正在编写代码以根据用户输入 (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”时停止。我需要的是当结果等于其中一个名称时停止循环的代码块。我英语不好,希望大家能理解。

【问题讨论】:

  • 请显示完整的代码我统计了至少四个未知变量。
  • data - 这是一个标志的坏名字 - 总是被设置为false,因为你的result不等于Lulu,它总是在列表中继续到Lulu。因此,只要您在result 中有一对,data 就会为假并且循环结束。但是你显示!data 而不是data,所以你认为它找到了平等。这里的整个逻辑是错误的。请重新考虑您的算法。
  • 好的,我会上传并更改一些代码,我没有,因为它是在印度尼西亚编写的。
  • 当'result'等于'names'数组之一时,我需要“一些东西”来停止循环。

标签: java arrays foreach


【解决方案1】:

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

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 的临时文件中运行它。

【讨论】:

  • 非常感谢您,您真的帮了大忙,希望您度过愉快的一周。
猜你喜欢
  • 2020-07-02
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 2011-01-06
相关资源
最近更新 更多