编写一个JAVA程序,按以下顺序打印:
输入:编写一个JAVA程序,按以下顺序打印:,java,Java,输入:{a,b,c,d}(字符数组) 我想使用上面的字符数组作为输入生成下面的序列 输出: a bcd ab cd abc d abcd a b c d ab c d a bc d a b cd 此代码适用于上述给定输出 for(i=0;i<(2*a.length);i++){ if(i<a.length){ for(j=0;j<a.length;j++){ System.out.print(
{a,b,c,d}
(字符数组)
我想使用上面的字符数组作为输入生成下面的序列
输出:
a bcd
ab cd
abc d
abcd
a b c d
ab c d
a bc d
a b cd
此代码适用于上述给定输出
for(i=0;i<(2*a.length);i++){
if(i<a.length){
for(j=0;j<a.length;j++){
System.out.print(a[j]);
if(j==i)
System.out.print(" ");//for white spaces
}
System.out.println(""); //new line
}
else{
for(k=0;k<a.length;k++){
System.out.print(a[k]);
if(k!=(i-(a.length+1)))
System.out.print(" ");
}
System.out.println("");
}
}
对于(i=0;i您可以尝试
char[] a = { 'a', 'b', 'c', 'd' };
for (int i = 0; i < (a.length*2); i++) {
if (i < a.length) {
for (int j = 0; j < a.length; j++) {
System.out.print(a[j]);
if (j == i)
System.out.print(" ");
}
System.out.println("");
} else {
for (int k = 0; k < a.length; k++) {
System.out.print(a[k]);
if (k != (i - (a.length + 1)))
System.out.print(" ");
}
System.out.println("");
}
}
char[]a={'a','b','c','d'};
对于(int i=0;i<(a.长度*2);i++){
如果(i
导入org.apache.commons.lang3.StringUtils;
公开课演示2{
公共静态void main(字符串…参数){
char[]a={'a','b','c','d'};
String str=String.valueOf(a);//转换为
字符串[]输入=str.split(“”;//字符串数组
String str1=StringUtils.join(输入“”;//在输出的前半部分连接到“abcd”
String str2=StringUtils.join(输入“”;//在输出的后半部分连接到“abcd”
for(inti=1;ifor(inti=(1=0;--i){
系统输出打印(a[0]);
对于(int j=0;j 如果((i&(1)请发布syso
方法代码。发布syso方法定义请提供正确的代码。即使在前4种情况下,您的代码也不能提供正确的输出!不,您仍然没有答案。我的答案与下一个答案相同,但我是第一个发布它的人。然而,我的评分很差。
import org.apache.commons.lang3.StringUtils;
public class Demo2 {
public static void main(String...args){
char [] a = {'a','b','c','d'};
String str = String.valueOf(a);//convert to
String[] input = str.split("");//string array
String str1 = StringUtils.join(input,""); // join to "abcd" for the first half of output
String str2 = StringUtils.join(input," "); // join to "a b c d" for the second half of output
for(int i = 1; i<str1.length()+1; i++){
System.out.println(str1.substring(0, i)+" "+str1.substring(i, str1.length())); //insert " "at index i
}
System.out.println(str2);
for(int i = 1; i<str2.length()-1; i=i+2){
System.out.println(str2.substring(0, i)+""+str2.substring(i+1, str2.length())); //remove " " at index i
}
}
}
for (int i = (1 << (a.length - 1)) - 1; i >= 0; --i) {
System.out.print(a[0]);
for (int j = 0; j < a.length - 1; ++j) {
if ((i & (1 << j)) == 0)
System.out.print(" ");
System.out.print(a[j + 1]);
}
System.out.println();
}