1281A-Suffix Three(endWith()、charAt()、substring()方法)

该博客介绍了1281A-Suffix Three问题的解决方案,通过Java中String类的endWith()、charAt()和substring()方法判断字符串后缀,以确定代表的国家。
摘要由CSDN通过智能技术生成

1281A-Suffix Three
题意:根据给出的字符串的后缀判断所代表的国家
题解:使用String类的endWith()或charAt()或substring()方法获取字符串的后缀

  1. endWith()方法
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        int t=scan.nextInt();
        //读入t所在的一整行
        scan.nextLine();
        while (t-->0){
            String str=scan.nextLine();
            int length=str.length();
            if(str.endsWith("po")){
                System.out.println("FILIPINO");
            }
            if(str.endsWith("desu")||str.endsWith("masu")){
                System.out.println("JAPANESE");
            }
            if (str.endsWith("mnida")){
                System.out.println("KOREAN");
            }
        }
    }
}
  1. charAt()方法
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        int t=scan.nextInt();
        //读入t所在的一整行
        scan.nextLine();
        while (t-->0){
            String str=scan.nextLine();
            int length=str.length();
            if(str.charAt(length-1)=='o'){
                System.out.println("FILIPINO");
            }
            if(str.charAt(length-1)=='u'){
                System.out.println("JAPANESE");
            }
            if (str.charAt(length-1)=='a'){
                System.out.println("KOREAN");
            }
        }
    }
}
  1. substring()方法
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        int t=scan.nextInt();
        scan.nextLine();
        while (t-->0){
            String str=scan.nextLine();
            int length=str.length();
            if(str.substring(length-2).equals("po")){
                System.out.println("FILIPINO");
            }
            else if(str.substring(length-4).equals("desu")||str.substring(length-4).equals("masu")){
                System.out.println("JAPANESE");
            }
            else if (str.substring(length-5).equals("mnida")){
                System.out.println("KOREAN");
            }
        }
    }
}

重点:

  1. public boolean endsWith(String suffix)
    判断字符串是否以指定的后缀结束,返回值为boolean型
  2. public char charAt(int index)
    返回指定索引处的字符。索引范围为从 0 到 length() - 1。
  3. public String substring(int beginIndex)或public String substring(int beginIndex, int endIndex)
    返回字符串的子字符串
    beginIndex – 起始索引(包括), 索引从 0 开始
    endIndex – 结束索引(不包括)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值