1281A-Suffix Three
题意:根据给出的字符串的后缀判断所代表的国家
题解:使用String类的endWith()或charAt()或substring()方法获取字符串的后缀
- 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");
}
}
}
}
- 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");
}
}
}
}
- 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");
}
}
}
}
重点:
- public boolean endsWith(String suffix)
判断字符串是否以指定的后缀结束,返回值为boolean型 - public char charAt(int index)
返回指定索引处的字符。索引范围为从 0 到 length() - 1。 - public String substring(int beginIndex)或public String substring(int beginIndex, int endIndex)
返回字符串的子字符串
beginIndex – 起始索引(包括), 索引从 0 开始
endIndex – 结束索引(不包括)