import java.util.Scanner; //Needed for the Scanner class
public class NumericTypes {
public static void main (String [] args) {
//TASK #2 Create a Scanner object here
//Reading from system.in
Scanner keyboard = new Scanner(System.in);
//prompt user for first name
System.out.println("Enter your first name: ");
//scans the next input as a double
String firstName = keyboard.nextLine();
//prompt user for last name
System.out.println("Enter your last name: ");
//scans the next input as a double
String lastName = keyboard.nextLine();
//concatenate the user's first and last names
String fullName = (firstName + " " + lastName);
//print out the user's full name
System.out.println(fullName);
//task 3 starts here
//get first initial from variable 'fullName'
String firstinitial = fullName.CharAt(0);
System.out.println("the first initial is: " + firstinitial);
}
}
我想要的输出是最后一行显示第一个名字的第一个首字母(用户输入)。任何帮助都会非常感激,非常感谢
答案 0 :(得分:1)
这可以通过两种方式完成 - :
1。)将 String firstinitial 替换为 char firstinitial
2.。)使用String.valueOf包装fullName.charAt(0),如下所示: String firstinitial = String.valueOf(fullName.charAt(0));
两者都可以正常工作。