(前言)我实际上认为这是一个简单的问题,但我是公平的 编程新手,所以对我来说有点令人沮丧。这个问题有关 解决了在显示执行代码时大写和小写字符串如何相互覆盖的问题。
以下代码:
import java.util.Scanner; //need for the scanner class
public class Manip
{
public static void main(String[] args)
{
String city; //to hold the user input
//create the scanner object here
Scanner keyboard = new Scanner(System.in);
//get the favorite city
//prompt the user to input the favorite city
System.out.print("Enter favorite city: ");
//read and store into city
city = keyboard.nextLine();
//display the number of characters.
System.out.print("String Length :" );
System.out.println(city.length());
//display the city in all upper case
System.out.println(city.toUpperCase() );
//Display the city in all lower case
System.out.println(city.toLowerCase() );
//Display the first character
keyboard.next();
}
}
代码的输出是 -
enter favorite city: dallas
String Length :6
dallas
所需的输出是 -
enter favorite city: dallas
String Length :6
DALLAS
dallas
我想知道为什么不打印"达拉斯"两次,大写和小写字母。它似乎覆盖了输入
答案 0 :(得分:0)
我按原样运行你的代码,并以大写和小写形式打印它,但我注意到你有一条评论说//Display the first character
,然后跟着keyboard.next()
。这是唯一不正确的部分。要打印字符串的第一个字母(在本例中为字符串city
),请使用以下行:System.out.println(city.substring(0,1).toUpperCase());
下面是输出的示例:
Enter favorite city: dallas.
String Length :6
DALLAS
dallas
D