【发布时间】:2016-03-29 17:53:04
【问题描述】:
我正在编写一个程序,要求用户以小写形式输入他们的姓氏,并询问他们是否希望将其输出为全部大写或仅首字母大写。我遇到的问题是将 charAt 与 toUpperCase 一起使用。
import java.util.Scanner;
//This imports the scanner class
public class ChangeCase {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
//This allows me to use the term scan to indicate when to scan
String lastName;
//sets the variable lastName to a string
int Option;
System.out.println("Please enter your last name");
//Prints out a line
lastName = scan.nextLine();
//scans the next line of input and assigns it to the lastName variable
System.out.println("Please select an option:"+'\n'+"1. Make all leters Capitalised"+'\n'+ "2. Make the First letter Capitalised");
Option = scan.nextInt();
if (Option == 1){
System.out.println(lastName.toUpperCase());
}
if (Option == 2){
System.out.println(lastName.charAt(0).toUpperCase());
}
}
}
我收到一条错误消息“无法在原始类型 char 上调用 toUpperCase()”
【问题讨论】:
-
请编辑您的问题,并粘贴您的代码文本(不是指向您的代码图像的链接)。粘贴后,选择文本并按 ctrl-k 以缩进必要的 4 个空格。