有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我想得到字符串中的最后一个字母,并根据字母打印true或false

我是编程新手,目前是我第一年尝试编程。 就像标题一样,我很难完成我的这段代码。 我已经到处找了,找不到什么

代码不完整,我知道我需要更改一些内容。 我能做些什么来完成这个?我已经筋疲力尽了

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) 个答案

  1. # 1 楼答案

    你想做什么很清楚,但是你可以通过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");
    }
    
  2. # 2 楼答案

            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");
            }
    
  3. # 3 楼答案

    我不是百分之百确定你想做什么,但我已经试着仔细检查你的代码似乎在做什么,因为你似乎有点不确定,然后提出我将如何解决我认为你的问题

    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");
         }
    
    
    
    }