java提取最后一个字符,怎么弄啊!急急急

Create a new class named Sentence.java. In the main method, write a program that reads a sentence using a dialog box. Depending on the last character of the sentence, output another dialog box identifying the sentence as declarative (ends with a period), interrogative (ends with a question mark), exclamatory (ends with an exclamation point), or other. Use a switch structure to accomplish this.

第1个回答  2015-03-12
先用lastIndexOf()得到最后字符的位置的‘值’,然后用substring(‘值’)提取
用法:
String s="Create a new class named Sentence.java. In the main method, write a program that reads a sentence using a dialog box. Depending on the last character of the sentence, output another dialog box identifying the sentence as declarative (ends with a period), interrogative (ends with a question mark), exclamatory (ends with an exclamation point), or other. Use a switch structure to accomplish this.“;
不知道你是要得到this还是最后的标点符号
如果是this:
int index=s.lastIndexOf("this");
String ss=s.substring(index,s.length()-1);
如果就是标点符号:
String ss=s.substring(s.length()-1);
ss就是你要的字符,纯手打希望帮助你。
第2个回答  推荐于2016-04-13
String s="需要被提取的字符。";
int start=s.length()-1;
int end=s.length();
String ss=s.subString(start,end);本回答被提问者和网友采纳
第3个回答  2015-03-12
题目意思是叫你通过最后一个标点来判断这是一个什么句子,然后在对话框中输出,测试代码如下:
public class Sentence {
public static void main(String[] args) {
String str ="hello!";
String identify =str.trim().substring(str.length()-1, str.length());//获取最后一个字符

switch (identify) {
case ".":System.out.println("declarative");;break;//陈述句
case "?":System.out.println("interrogative");break;//疑问句
case "!":System.out.println("exclamatory");break;//感叹句
default:break;
}
}

}
第4个回答  推荐于2018-05-12
String str = "hello";
System.out.print(str.charAt(str.length()-1));

第5个回答  2015-03-12
String string = "sdfsdfwerewrw";
System.out.println(string.charAt(string.length()-1));

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网