java ,句子中的单词按顺序输出
老师出了一个别扭的题。一共四个英文句子1Thebuswascrowdedandnovacantseatwastobefound.6529121411107382Iwill...
老师出了一个别扭的题。一共四个英文句子
1 The bus was crowded and no vacant seat was to be found.
6 5 2 9 12 1 4 11 10 7 3 8
2 I will never come to such an unpleasant place again.
2 8 6 10 9 1 5 3 7 4
3 My family shall not want for anything as long as I live.
7 2 12 10 3 8 4 9 6 11 5 1
4 I have just been to the hospital to inquire after Mr.A.
11 4 2 9 7 10 1 6 3 5 8
首先,在屏幕上输出这四句话,然后问用户选择哪句话,然后 按句子下面的数字,一次输出对应位置的单词。如 输出3 ,则显示
anything family live as shall as not long for I want My
请高手帮忙,谢谢 展开
1 The bus was crowded and no vacant seat was to be found.
6 5 2 9 12 1 4 11 10 7 3 8
2 I will never come to such an unpleasant place again.
2 8 6 10 9 1 5 3 7 4
3 My family shall not want for anything as long as I live.
7 2 12 10 3 8 4 9 6 11 5 1
4 I have just been to the hospital to inquire after Mr.A.
11 4 2 9 7 10 1 6 3 5 8
首先,在屏幕上输出这四句话,然后问用户选择哪句话,然后 按句子下面的数字,一次输出对应位置的单词。如 输出3 ,则显示
anything family live as shall as not long for I want My
请高手帮忙,谢谢 展开
7个回答
展开全部
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Test test1 = new Test("The bus was crowded and no vacant seat was to be found.",6,5,2,9,12,1,4,11,10,7,3,8);
Test test2 = new Test("I will never come to such an unpleasant place again.",2,8,6,10,9,1,5,3,7,4);
Test test3 = new Test("My family shall not want for anything as long as I live.",7,2,12,10,3,8,4,9,6,11,5,1);
Test test4 = new Test("I have just been to the hospital to inquire after Mr.A.",11,4,2,9,7,10,1,6,3,5,8);
Test[] tests = {test1,test2,test3,test4};
for (int i = 0; i < tests.length; i++) {
System.out.println(i+1+" : "+tests[i].getSentence());
}
System.out.println("请选择你要输出的话:");
int i = input.nextInt();
System.out.println(tests[i-1]);
}
private String sentence;
private int[] indexs;
public int[] getIndexs() {
return indexs;
}
public void setIndexs(int[] indexs) {
this.indexs = indexs;
}
public Test() { }
public Test(String sentence, int...indexs) {
this.sentence = sentence;
this.indexs = indexs;
}
public String getSentence() {
return sentence;
}
public void setSentence(String sentence) {
this.sentence = sentence;
}
public String toString() {
String[] words = sentence.split(" ");
StringBuffer newSentence = new StringBuffer();
for (int i = 0; i < indexs.length; i++) {
newSentence.append(words[indexs[i]-1]+" ");
}
return newSentence.toString();
}
}
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Test test1 = new Test("The bus was crowded and no vacant seat was to be found.",6,5,2,9,12,1,4,11,10,7,3,8);
Test test2 = new Test("I will never come to such an unpleasant place again.",2,8,6,10,9,1,5,3,7,4);
Test test3 = new Test("My family shall not want for anything as long as I live.",7,2,12,10,3,8,4,9,6,11,5,1);
Test test4 = new Test("I have just been to the hospital to inquire after Mr.A.",11,4,2,9,7,10,1,6,3,5,8);
Test[] tests = {test1,test2,test3,test4};
for (int i = 0; i < tests.length; i++) {
System.out.println(i+1+" : "+tests[i].getSentence());
}
System.out.println("请选择你要输出的话:");
int i = input.nextInt();
System.out.println(tests[i-1]);
}
private String sentence;
private int[] indexs;
public int[] getIndexs() {
return indexs;
}
public void setIndexs(int[] indexs) {
this.indexs = indexs;
}
public Test() { }
public Test(String sentence, int...indexs) {
this.sentence = sentence;
this.indexs = indexs;
}
public String getSentence() {
return sentence;
}
public void setSentence(String sentence) {
this.sentence = sentence;
}
public String toString() {
String[] words = sentence.split(" ");
StringBuffer newSentence = new StringBuffer();
for (int i = 0; i < indexs.length; i++) {
newSentence.append(words[indexs[i]-1]+" ");
}
return newSentence.toString();
}
}
展开全部
用一个switch 语句啊
switch(输入的数字){
case 1:
System.out.println(" The bus was crowded and no vacant seat was to be found.
")
break;
case 2:
System.out.println(" I will never come to such an unpleasant place again.
")
break;
以此类推。。
}
switch(输入的数字){
case 1:
System.out.println(" The bus was crowded and no vacant seat was to be found.
")
break;
case 2:
System.out.println(" I will never come to such an unpleasant place again.
")
break;
以此类推。。
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以先使用switch语句判断是那句话,然后可以通过定义一个方法将选择的句子当作字符串作为一个参数传到方法中
方法代码如下:
public String getWords(String words,int []pos)
{//words参数代表用户选择的那句话,pos 表示要输出的单词的所有位置的一个数组
String[] str = words.split(" ");
StringBuffer sbu = new StringBuffer();
for(int i=0;i<pos.length;i++)
{
if(i>0)
sbu.append(" ");
sbu.append(str[pos[i]])
}
return sbu.toString();
}
switch方法伪代码:
switch(输入的数字){
case 1:
System.out.println(getWords("The bus was crowded and no vacant seat was to be found",new int[]{输出单词的位置所构成的数组}));
break;
case 2:
同case1
break;
后面以此类推。。
}
……总算敲完了,分给我吧
方法代码如下:
public String getWords(String words,int []pos)
{//words参数代表用户选择的那句话,pos 表示要输出的单词的所有位置的一个数组
String[] str = words.split(" ");
StringBuffer sbu = new StringBuffer();
for(int i=0;i<pos.length;i++)
{
if(i>0)
sbu.append(" ");
sbu.append(str[pos[i]])
}
return sbu.toString();
}
switch方法伪代码:
switch(输入的数字){
case 1:
System.out.println(getWords("The bus was crowded and no vacant seat was to be found",new int[]{输出单词的位置所构成的数组}));
break;
case 2:
同case1
break;
后面以此类推。。
}
……总算敲完了,分给我吧
追问
编译不过,能贴出全部代码么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
4个String数组 switch()判断是哪个数组 然后for(int i=String.length()-1;i>=0;i--)光要代码是没用的 没逻辑 要代码 也是白要
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的题目有问题少年。 根本没什么逻辑可言 直接switch System.out.println("...");吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
本来要写的 一看三楼就知道有高手看过了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询