gpt4 book ai didi

Java无法正确显示字符串的最后一个字母?

转载 作者:行者123 更新时间:2023-12-01 18:42:24 24 4
gpt4 key购买 nike

所以我有这个小程序,它所需要做的就是检查姓氏的最后一个字母是否是“s”。如果是“s”,则会将姓氏更改为复数。
例如
史密斯=史密斯的
史密斯=史密斯'

将姓氏更改为复数。简单吧?看起来是这样,但我的 if 语句没有检测最后一个字母是否是“s”

这是一些代码

import javax.swing.JOptionPane;


public class Lastname {
public static void main(String[] args) {

String messageText = null;
String title = null;
int messageType = 0;
String lastName = "";
String pluralLastName = "";

Input input;

input = new Input();

messageText = "Please enter a last name. I'll make it plural.";
title = "Plural Last Names";
messageType = 3;

lastName = input.getString(messageText,title,messageType);



int intLength = lastName.length();
String lastLetter = lastName.substring(intLength- 1);
System.out.println("The last letter is: " + lastLetter);

if (lastLetter.equals('s'))
JOptionPane.showMessageDialog(null, "The last name entered as plural is " + lastName + "'" );
else
JOptionPane.showMessageDialog(null, "The last name entered as plural is " + lastName + "'s" );




}}

if 语句总是只在所有内容上添加一个“'s”。

最佳答案

您需要使用双引号来表示 String 文字。

if (lastLetter.equals("s"))

否则,您将 StringCharacter 进行比较,后者将始终返回 false

关于Java无法正确显示字符串的最后一个字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19437390/

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