【问题标题】:Java program to find largest ascii character in stringJava程序在字符串中查找最大的ascii字符
【发布时间】:2016-05-22 22:50:56
【问题描述】:

您好,我需要编写一个程序,让用户输入名称(例如)并在字符串中找到最大值的 ascii 字符,然后将其输出给用户。我已经开始了,但我不确定从哪里继续。感谢您的帮助。

System.out.println("Enter your name: ");
String name = input.nextLine();
char ch = ' ';
int i;

for (i = 0; i < name.length(); i++)
{

}

【问题讨论】:

  • 您想从第一个字符开始,并将下一个字符与此进行比较。如果第二个更大,那么您想将其用作向前移动的基础
  • 没有“ascii 字符”这样的东西; ASCII 是一种字符编码。你想要的是最高的代码点。使用 Java 8 就像 theString.codePoints().max().orElse(-1) 一样简单
  • @fge:有点跑题了:当然有 ASCII 字符(当然取决于您如何解释该术语)。 ASCII 是一种字符编码方案,有效的“ASCII 字符”可以简单地表示存在于 ASCII 标准中的值。尽管在 Java 中处理代码点更有意义,因为它是基于 Unicode(这只是另一种字符编码方案)构建的,但处理 ASCII 仍然有效。鉴于 Unicode 代码点与代码点
  • 查找用户输入的最大字符,该字符为
  • @AdrianShum 不,Unicode 不是字符编码!它是一个代码点列表;但它确实定义了字符编码(UTF-* 系列;UTF 表示 Unicode 转换格式)

标签: java


【解决方案1】:

你可以从这个 sn-p 开始

// the String to analyze
String name = "foobar";
// use the lowest character as initial value
char greatestChar = (char) 0;
// iterate over all characters in the string
for (int i = 0; i < name.length(); i++) {
    // if the current character is bigger then the actual stored greatest ...
    if (name.charAt(i) > greatestChar) {
        // ... then keep the actual one as greatest
        greatestChar = name.charAt(i);
    }
}
System.out.println("greatestChar = " + greatestChar);

编辑一个使用 Stream API 的 sn-p。

String name = "foobar";
char greatestChar = (char) name.chars().max().getAsInt();
    // .chars() - returns a stream of int
    // .max() - return the maximum element in the stream (which is an OptionalInt)
    // .getAsInt() - return the value of the OptionalInt as int
System.out.println("greatestChar = " + greatestChar);

【讨论】:

    【解决方案2】:
    String name = "Just for Testing";
    int greatestVal = 0;
    
    for (int i = 0; i < name.length(); i++)
    {
        int curVal = (int)name.charAt(i);
        if(curVal > greatestVal)
            greatestVal = curVal;
    }
    char asChar = (char)greatestVal;
    System.out.println("The character with the highest ASCII (encoding) number was " + asChar + " (with ASCII (encoding) " + greatestVal + ")");
    

    现在稍微解释一下:

    int curVal = (int)name.charAt(i); 行发生的事情称为显式转换.charAt() 返回一个char,我们将其转换为int(在它前面使用(int))。这就是“魔法”发生的地方。 curVal 现在在平台的默认编码中包含位置 i 处字符的相应值(更多信息 see this post)。几乎所有这些编码的共同点是前 128 个字符与 ASCII 匹配,更多细节in this question

    现在可以将其与greatestVal 进行比较,通过循环整个字符串,我们可以找到具有最高底层ASCII 值的字符。这里有一个an ASCII table的链接,所以你可以自己检查哪个字符对应哪个值。

    char asChar = (char)greatestVal;,我们只是再次做同样的事情,我们将int转换char,然后可以打印出来。


    如另一个答案所示,您还可以将chars 相互比较,例如name.charAt(i) &gt; greatestChar。后面发生的事情是,这两个字符是根据它们的编码值进行比较的,就像我在上面的示例中手动向您展示的那样。

    【讨论】:

    • Java 的 charCharacterstring 使用 Unicode/UTF-16。这里不是 ASCII,而是指向 Unicode table 的链接。 (注意值是按照约定的十六进制。)
    • 所以,更新了它并添加了指向其他两个处理编码问题的链接。
    • 您对此做了一些很好的研究,但它比这更简单。 Java(如 JavaScript、.NET 等)通常自始至终使用 Unicode;内存中的 UTF-16 和流和文件的 UTF-8。 charAt 在索引处给出 UTF-16 代码单元。平台默认编码可以潜入一些方法,但最好避免使用这些方法。在不需要 Unicode 的特殊情况下,通常首选特定编码而不是“平台默认值”。 ASCII 很少是平台默认值。
    • 不需要从charint 的转换。因为会有一个隐含的widening primitive conversion jls 5.1.2。所以你可以直接比较currentCahr &gt; greatestChar,其中两个变量都是char
    【解决方案3】:

    这样的事情会起作用

    String str = "someString"; 
    char[] charArray = str.toCharArray();
    int max = 0;
    for(char c : charArray){
        max = Math.max(max, (int) c);
    }
    System.out.println("Value: " + max);
    System.out.println(Character.toString((char) max));
    

    【讨论】:

      【解决方案4】:

      在 Java 8+ 中,这可以通过单行非常简单地完成:

      String str = "TestString"; // Expected result: 'S'
      char largestChar = (char)str.chars().min().getAsInt();
      

      或者如果你想打印它:

      String str = "TestString"; // Expected result: 'S'
      System.out.printf("Largest ASCII-value is: '%c'", str.chars().min().getAsInt());
      

      Try it online.

      【讨论】:

        猜你喜欢
        • 2014-03-28
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多