gpt4 book ai didi

java - 打印输入行中有多少对第一个字符小于第二个字符的字符

转载 作者:行者123 更新时间:2023-11-30 06:15:33 24 4
gpt4 key购买 nike

我不知道如何比较字符 char < Char 并添加计数

它应该打印输入一行:反建制主义\(无论用户想输入什么)

你的答案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);
}
}
}

我不能使用 HashMap 或任何其他类型的循环。

最佳答案

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

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 );

关于java - 打印输入行中有多少对第一个字符小于第二个字符的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49286414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com