我想获取字符串中的最后一个字母并根据字母打印true或false

时间:2018-12-15 07:12:28

标签: java

我是编程新手,目前是我尝试该课程的第一年。 就像标题一样,我很难完成我的代码。 我已经搜索了高低,找不到任何东西。

代码不完整,我知道我需要更改一些内容。 我该怎么做才能做到这一点?我已经累了。

import java.util.*;
public class Ihateyou {
    public static void main (String []args) {
        Scanner fkc = new Scanner (System.in);
            System.out.println("Enter an Element or something: ");
            char ch=fkc.next( ).charAt(0);
            String unknown = fkc.nextLine();
            if (ch=='H' + unknown.substring(unknown.length() - 1))
            {
                System.out.println("False  "+ch+" ends with h");
            }
            else if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
                System.out.println("True "+ch+" ends with no H");


    }
}

3 个答案:

答案 0 :(得分:0)

现在还不清楚要做什么,但是可以使用str.charAt(str.length() - 1)获得的字符串的最后一个字母:

try (Scanner scan = new Scanner(System.in)) {
    System.out.print("Enter an Element or something: ");
    String str = scan.nextLine();
    char lastChar = str.charAt(str.length() - 1);

    if (lastChar != 'h')
        System.out.println("False " + str + " ends with h");
    else if (Character.isLetter(lastChar))
        System.out.println("True " + str + " ends with no H");
}

答案 1 :(得分:0)

我不确定您要做什么,但我尝试通过代码似乎正在做的事情来尝试,因为您似乎对此不太确定,然后提出我将如何解决该问题的建议,请100%确定我想你有。

public class Ihateyou {
    public static void main (String []args) {
        Scanner fkc = new Scanner (System.in); // This creates a new scanner, which lets us read from System
            System.out.println("Enter an Element or something: "); 
            char ch=fkc.next( ).charAt(0); // This reads the next sequence terminated by the enter key. Then it stores the single first character of that in a char, ch
            String unknown = fkc.nextLine(); // This basically does the same thing, storing it in unknown
            if (ch=='H' + unknown.substring(unknown.length() - 1)) // This checks if ch equals H + most of the unknown string. This doesn't make sense for two reasons.
                                   // One problem is that 'H' + unknown is a string, not a char: it doesn't make sense to compare a String with a char.
                                   // The other is that unless unknown is an empty string (""), and char is 'H', even if we could compare they wouldn't be the same
            {
                System.out.println("False  "+ch+" ends with h");
            }
            else if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
                System.out.println("True "+ch+" ends with no H");


    }
}

我认为您可能要尝试确定用户输入的内容是否以H结尾。为此,我将尝试以下操作:

public static void main (String []args) {
    Scanner fkc = new Scanner (System.in); // This creates a new scanner, which lets us read from System
        System.out.println("Enter an Element or something: "); 
    String userInput = fkc.nextString(); // Store the next String the user inputs in userInput
    if(userInput.charAt(userInput.length() - 1)) == 'H'){  // userInput.charAt() gets us the character at a given index in the string. We want the last one, so we get 
                               // the one at userInput.length() - 1. Then we check to see if it is H
    {
            System.out.println("False  "+ch+" ends with h");
        }
        else{
            System.out.println("True "+ch+" ends with no H");
     }



}

答案 2 :(得分:0)

        Scanner fkc = new Scanner (System.in);
        System.out.println("Enter an Element or something: ");

        String unknown = fkc.nextLine();

        char [] arr = unknown.toCharArray();

        int lastLetterIndex = arr.length - 1;

        if(arr[lastLetterIndex]=='h') {

            System.out.println("ends with H");
        }

        else {

            System.out.println("ends with no H");
        }