将java数组元素转换为句子
我需要获取数组中的元素,并使用它生成一条输出消息,如“您的句子是(元素将去的地方)”。但我知道的唯一方法是使用for循环将java数组元素转换为句子,java,arrays,Java,Arrays,我需要获取数组中的元素,并使用它生成一条输出消息,如“您的句子是(元素将去的地方)”。但我知道的唯一方法是使用for循环 private void sentanceArray() { int size = 0; String word = ""; int i = 0; Scanner input = new Scanner(System.in); System.out.println("How many wor
private void sentanceArray() {
int size = 0;
String word = "";
int i = 0;
Scanner input = new Scanner(System.in);
System.out.println("How many words would you like to enter?");
size = input.nextInt();
String [] sentance = new String[size];
System.out.println("Please enter a word.");
word = input.next();
while( i < size){
if( i < size){
sentance[i] = word;
i++;
System.out.println("Please enter another word.");
word = input.next();
}//end of if
}//end of while
displayArray(sentance);
} // end of sentanceArray
public static void displayArray(String []sentance){
for(int i = 0; i < sentance.length; i++){
System.out.print( "Your sentance is " + sentance[i] +"\t" );
}
}//end of displayResults
private void sentanceArray(){
int size=0;
字串=”;
int i=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“您想输入多少单词?”);
size=input.nextInt();
字符串[]语句=新字符串[大小];
System.out.println(“请输入一个单词”);
word=input.next();
而(i
我的问题是它显示“Your sentance is”,然后一次只显示一个元素请尝试以下操作:
System.out.print("Your sentence is ");
for(String x : sentance)
System.out.print(x + "\t");
而且
while(i
那你为什么不在for循环中执行呢?你应该做的一件事是将“你的句子是”移动到循环之前,否则它会打印多次。谢谢,我很难过,我没有想到移动它会起作用!很抱歉信息不足,这是第一次执行此操作
while( i < size){
if( i < size){ // You dont need this condition, while loop checks for that