【问题标题】:Print how many pairs of characters in the input line where the first character is less than the second打印输入行中第一个字符小于第二个字符的字符对
【发布时间】:2018-03-14 19:40:27
【问题描述】:

我不确定如何比较字符 char

它应该打印 输入一行: antidisestablishmentarianism\(用户想输入的任何内容)

你的答案 15

import java.util.Scanner;

public class CountRisingPairs {

    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);       
        System.out.println(" Enter a string");      

        String S=in.next();      
        int count;
        int value;
           for (int i=65; i<91; i++) {
              count=0;
              for (int j=0; j<in.length(); j++) {
              value=(int)in[j];
              if (value == i) {
                 count++;
              }
           }
           if (count>0) 
              System.out.println((char)i+" -- "+count);
        }
    }
}

我不能使用哈希映射或任何其他类型的循环。

【问题讨论】:

    标签: java for-loop computer-science


    【解决方案1】:

    循环应该遍历从第一个到下一个但最后一个的所有字符,以便您可以比较相邻的字符。它们可以像整数值一样进行比较。

    String s = "antidisestablishmentarianism";
    int count = 0;
    for( int i = 0; i < s.length() - 1; ++i ){
        if( s.charAt(i) < s.charAt(i+1) ) count++;
    }
    System.out.println( "count = " + count );
    

    【讨论】:

      【解决方案2】:

      为了比较输入中的字符,您可能应该保留一个与前一个字符进行比较的变量。我不认为与索引变量 i 进行比较是您想要的。那么你的 if 语句将类似于

      if (value > previous) {
          count++;
      }
      

      此外,当遍历扫描器的输入时,您可能应该使用这样的 while 循环:

      while (in.hasNext()) {
          // Your counting here
      }
      

      您需要一种方法来终止该 while 循环 - 您可以通过检查 '\n' 或其他内容来实现。当然,如果您愿意,可以将 while 循环重写为 for 循环。

      【讨论】:

      • 其他类型的循环,例如不允许while循环
      【解决方案3】:
      String element = "antidisestablishmentarianism";
      
      int count = 0;
      for (int j=0; j<element.length(); j++)
      {
        if(j+1 < element.length()){
          int x = Character.getNumericValue(element.charAt(j));
          int y = Character.getNumericValue(element.charAt(j+1));
          if(x>y){
              count++;
              System.out.println("Pair: "+element.charAt(j)+""+element.charAt(j+1));
          }
        }
      
      
      }
      
      System.out.println(count+" pairs found");
      

      【讨论】:

      • 我实现了你的代码,它是正确的。唯一从提示中抛出代码的是 if (x > y) 应该是
      【解决方案4】:

      根据您的代码,同时使用 try-with-resources 关闭扫描仪:

      import java.util.Scanner;
      
      public class CountRisingPairs {
      
          public static void main(String[] args) {
              try (Scanner in = new Scanner(System.in)) {
                  System.out.println(" Enter a string");
      
                  String inputString = in.next();
                  int count = 0;
                  char previousChar = 100;
      
                  for (char currentChar : inputString.toCharArray()) {
                      if (currentChar > previousChar) {
                          count++;
                      }
                      previousChar = currentChar;
                  }
                  System.out.println(count);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        • 2022-08-23
        • 2016-06-09
        • 2013-02-08
        • 2018-04-04
        相关资源
        最近更新 更多