java我怎么把输入的五个字符串连在一起输出出来
publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);Stringchar2;for...
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String char2;
for(int i=1;i<=5;i++){
System.out.println("请输入第"+i+"个字符串:");
String char1=input.next();
}
System.out.println("组合为");
}
} 展开
Scanner input=new Scanner(System.in);
String char2;
for(int i=1;i<=5;i++){
System.out.println("请输入第"+i+"个字符串:");
String char1=input.next();
}
System.out.println("组合为");
}
} 展开
4个回答
展开全部
package string;
import java.util.Scanner;
public class ConcatString {
public static void main(String[] args) {
System.out.println("请输入字符串1:");
Scanner sc = new Scanner(System.in);
String s0 = sc.nextLine();
StringBuilder sb = new StringBuilder();
sb.append(s0);
System.out.println("请输入字符串2:");
String s1 = sc.nextLine();
sb.append(s1);
System.out.println("请输入字符串3:");
String s2 = sc.nextLine();
sb.append(s2);
System.out.println("请输入字符串4:");
String s3 = sc.nextLine();
sb.append(s3);
System.out.println("请输入字符串5:");
String s4 = sc.nextLine();
sb.append(s4);
System.out.println("最后输出结果:"+sb.toString());
}
}
输出结果:
展开全部
你好,请参考
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//初始化char2
String char2 = "";
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "个字符串:");
String char1 = input.next();
//加到char2
char2 += char1;
}
//输出到char2
System.out.println("组合为" + char2);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先,你这么写可能不对的,JAVA索引应该是从0开始
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String char2;
String result=“”;
for(int i=1;i<=5;i++){
System.out.println("请输入第"+i+"个字符串:");
String char1=input.next();
result=result+char1;
}
System.out.println("组合为"+result);
}
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String char2;
String result=“”;
for(int i=1;i<=5;i++){
System.out.println("请输入第"+i+"个字符串:");
String char1=input.next();
result=result+char1;
}
System.out.println("组合为"+result);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-12-02
展开全部
public static void main(String[] args) {
Scanner input = new Scanner(System. in );
String char2 = "";
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "个字符串:");
String char1 = input.next();
char2 += char1;
}
input.close();
System.out.println("组合为: " + char2);
}
Scanner input = new Scanner(System. in );
String char2 = "";
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "个字符串:");
String char1 = input.next();
char2 += char1;
}
input.close();
System.out.println("组合为: " + char2);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询