【问题标题】:Comparing a string outside a loop with one outside the loop in java比较循环外的字符串与java中循环外的字符串
【发布时间】:2014-03-03 03:55:42
【问题描述】:

我正在编写一个程序,该程序从用户那里获取一个单词,例如“banana”,将单词“b”的第一个字母像 ananab 一样放在末尾,然后检查它是否拼写相同的单词。我已经为此工作了几天,并尝试了几件事,但仍然不确定如何检查用户给出的字符串,其中一个字符串位于 for 循环中。到目前为止,这是我的程序。

public static void main(String[] args) {

    System.out.println("Enter words that can be checked for backward spelling");
    System.out.println("Please enter a word to check");
    Scanner keyboard = new Scanner(System.in);

    String words = keyboard.nextLine();
    String firstLetter = String.valueOf(words.charAt(0));
    String words2 = words.substring(1);
    String otherwords = words2+firstLetter;


    for (int i=otherwords.length()-1; i>=0; i--){
        String newwords=String.valueOf(otherwords.charAt(i));
        boolean match = newwords.equalsIgnoreCase(words);
        if (match){
            System.out.println("This word matches the criteria we are lookin for");}
        }
}
}

【问题讨论】:

    标签: java string for-loop compare


    【解决方案1】:
    public static void main(String []args){
    
    String input = new Scanner(System.in).nextLine();
    
    char addToEnd = input.charAt(0);
    String newString = input.substring(1);
    newString+=addToEnd;
    
    if(input.equals(newString)){
    System.out.println("This is a match");
    
    }
    

    【讨论】:

    • 好的,谢谢...我添加了循环,因为一旦我在末尾添加了字母,我需要反转字符串,否则它不会是同一个词。这将是 ananab 而不是香蕉 有没有另一种方法可以在没有循环的情况下反转字符串,我之前尝试过 String buffer 我可能不得不再次使用它?
    • 不需要把字符串倒过来。我们从未失去它。如果用户输入“banana”,则字符串输入将是“banana”,而字符串 newString 将是“ananab”。在 if 语句之后,我们仍然有字符串输入...
    【解决方案2】:

    试试这个:

    public static void main(String[] args) {
    
        System.out.println("Enter words that can be checked for backward spelling");
        System.out.println("Please enter a word to check");
        Scanner sc = new Scanner(System.in);
        String word = sc.nextLine();
        StringBuilder sb = new StringBuilder();
        sb.append(word.substring(1));
        sb.append(word.charAt(0));
        System.out.println(sb.toString().equals(word)?"This is a match.":"This is not a match.");
    }
    

    【讨论】:

      【解决方案3】:
      System.out.println("Enter words that can be checked for backward spelling");
      System.out.println("Please enter a word to check");
      Scanner keyboard = new Scanner(System.in);
      String word = keyboard.nextLine();
      keyboard.close();
      String firstLetter = word.substring(0, 1);
      String newWord = word.substring(1) + firstLetter;
      // reverse the word
      String reversedWord = "";
      for (int i = 0; i < word.length(); i++) {
          reversedWord = word.substring(i, i + 1) + reversedWord;
      }
      // check if it matches
      if (reversedWord.equalsIgnoreCase(newWord)) {
          System.out.println("This word matches the criteria we are looking for!!!");
      }
      

      【讨论】:

      • 德罗巴,谢谢你,这太棒了,我要通读台词,用别的东西练习,所以我明白了!!!
      猜你喜欢
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多