gpt4 book ai didi

java - 打印出数组中所选项目的特定字母(Java)

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:04 24 4
gpt4 key购买 nike

我对 Java 非常非常陌生,所以如果这是我看不到的非常明显的事情,我深表歉意。我正在大学学习 Java 类(class),我们刚刚开始介绍数组。我们被分配了一个简单的练习:

Write a program that creates an array (names) with the names Bob, Ann & Tom. The program should go through the array and print out the first letter of each name as a word ("BAT").

我们提供了一种使用 for 循环来完成此操作的方法,并且该方法有效:

String [] names = {"Bob", "Ann", "Tom"};
String initials = "";
for (int i = 0; i < names.length; i++)
{
initials = initials + names[i].charAt(0);
}
System.out.print(initials);

但我想以另一种方式做到这一点,使用 char.At() 方法:

  System.out.print(names[0].charAt(0) + names[1].charAt(0) + names[2].charAt(0) + "\n");

但是,输出不是“BAT”,而是 215。任何人都可以解释为什么会发生这种情况以及我的代码有什么问题吗?非常非常感谢!

====编辑:

我现在已经尝试了 chsdk、jgr208、Pier-Alexandre Bouchard 和 Gorfield 建议的四种解决方案,但是它们都不起作用。我仍然得到 215。我还尝试了 this thread 中提到的将字符转换为字符串的方法。 ;我最终仍然得到 215。这让我思考也许我的数组声明有问题?有什么建议么?我在下面尝试过的代码详细信息。

String [] names = {"Bob", "Ann", "Tom"};
String initials = "";
initials = initials + names[0].charAt(0);
initials = initials + names[1].charAt(0);
initials = initials + names[2].charAt(0);
System.out.print(initials);

= 215

String [] names = {"Bob", "Ann", "Tom"};
String a = names[0].charAt(0) + "";
String b = names[1].charAt(0) + "";
String c = names[2].charAt(0) + "";
System.out.print("" + a + "" + b + "" + c + "");

= 215

String [] names = {"Bob", "Ann", "Tom"};
String a = new Character(names[0].charAt(0)).toString();
String b = new Character(names[1].charAt(0)).toString();
String c = new Character(names[2].charAt(0)).toString();
System.out.print("" + a + "" + b + "" + c + "");

= 215

String [] names = {"Bob", "Ann", "Tom"};
String a = Character.toString(names[0].charAt(0));
String b = Character.toString(names[1].charAt(0));
String c = Character.toString(names[2].charAt(0));
System.out.print("" + a + "" + b + "" + c + "");

= 215

String [] names = {"Bob", "Ann", "Tom"};
String a = String.valueOf(names[0].charAt(0));
String b = String.valueOf(names[1].charAt(0));
String c = String.valueOf(names[2].charAt(0));
System.out.println(a + "" + b + "" + c);

= 215

String [] names = {"Bob", "Ann", "Tom"};
System.out.printf("%s%s%s",names[0].charAt(0),names[1].charAt(0),names[2].charAt(0));

= 215

String [] names = {"Bob", "Ann", "Tom"};
System.out.print(names[0].substring(0));
System.out.print("\n");
System.out.print(names[1].substring(0));
System.out.print("\n");
System.out.print(names[2].substring(0));
System.out.println("");

= 215

String [] names = {"Bob", "Ann", "Tom"};
String initials = "";
initials+=names[0].charAt(0);
initials+=names[1].charAt(0);
initials+=names[2].charAt(0);
System.out.print(initials);

= 215

String [] names = {"Bob", "Ann", "Tom"};
System.out.print("" + names[0].charAt(0) + "" + names[1].charAt(0) + "" + names[2].charAt(0) + "\n");`

= 215

最佳答案

215 是 B+A+T (66+65+84) 的 ASCII 求和,基本上它认为您不是连接字符而是将它们相加。您可以使用 printf() 而不是普通的打印来解决这个问题:

System.out.printf("%s%s%s",names[0].charAt(0),names[1].charAt(0),names[2].charAt(0));

关于java - 打印出数组中所选项目的特定字母(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976793/

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