java中的int和char连接

java中的int和char连接,java,Java,此程序接收一个单词作为输入,如果单词长度大于10,则应打印单词的第一个字母,然后打印第一个字母和最后一个字母之间的字符数,然后打印最后一个字母。像“简介”这样的输入应该输出i10n。然而,当我尝试连接它们时,出现了一些问题,所以它只输出224,我不知道为什么。为什么会发生这种情况,我如何解决这个问题?任何帮助都将不胜感激 import java.util.Scanner; class Main { public static void main(String[] args) {

此程序接收一个单词作为输入,如果单词长度大于10,则应打印单词的第一个字母,然后打印第一个字母和最后一个字母之间的字符数,然后打印最后一个字母。像“简介”这样的输入应该输出i10n。然而,当我尝试连接它们时,出现了一些问题,所以它只输出224,我不知道为什么。为什么会发生这种情况,我如何解决这个问题?任何帮助都将不胜感激

import java.util.Scanner;

class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    for (int i = 0; i < n; i++){
      String word = sc.next();

      if (word.length() > 10){
        int lettersInBetween = (word.length() - 2);
        char firstChar = word.charAt(0);
        char lastChar = word.charAt(word.length() - 1);
        System.out.println(firstChar + lettersInBetween + lastChar);
      }
      else {
        System.out.println(word);
      }
    }
  }
}
import java.util.Scanner;
班长{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
对于(int i=0;i10){
int-lettersInBetween=(word.length()-2);
char firstChar=word.charAt(0);
char lastChar=word.charAt(word.length()-1);
System.out.println(firstChar+lettersInBetween+lastChar);
}
否则{
System.out.println(word);
}
}
}
}
试试这个:

    System.out.println(firstChar + Integer.toString(lettersInBetween) + lastChar);
关于数字224的输出,这很好地解释了这一点:

“例如,在ASCII编码中,小写字母i将由二进制1101001=十六进制69(i是第九个字母)=十进制105表示。”

只需替换行:

System.out.println(firstChar+lettersInBetween+lastChar)

在下面一行:


System.out.println(“+firstChar+lettersInBetween+lastChar”)

在Java中连接多个字符串和数值的最简单方法。请记住,当您有两个或两个以上的基元类型值(例如char、short或int)时,在字符串连接的开始处,您需要显式地将第一个基元类型值转换为字符串

String.valueOf(int i)
方法将整数值作为参数,并返回表示int参数的字符串

Integer.toString(inti)
方法的工作原理与String.valueOf(inti)方法相同。它属于Integer类,并将指定的整数值转换为字符串。例如,如果传递的值为101,则返回的字符串值将为“101”

可以使用这两种方法将整数转换为字符串

    int lettersInBetween = (word.length() - 2);
    char firstChar = word.charAt(0);
    char lastChar = word.charAt(word.length() - 1);
    String number = String.valueOf(lettersInBetween);
    System.out.println(firstChar + number + lastChar);


这回答了你的问题吗?
    int lettersInBetween = (word.length() - 2);
    char firstChar = word.charAt(0);
    char lastChar = word.charAt(word.length() - 1);
    String number = Integer.toString(lettersInBetween);
    System.out.println(firstChar + number + lastChar);