我的程序遇到问题,非常令人沮丧!我一直试图解决这个问题,但我不能帮助我。我所能做的就是这个
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter height: ");
int h = s.nextInt();
System.out.print("Enter width: ");
int w = s.nextInt();
for (int i=1; i<=h; i++) {
for (int j=1; j<=w; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
我的程序输出是:
Enter height: 4
Enter width: 4
****
****
****
****
所需的输出应为:
Enter height: 4
Enter with: 4
++++
****
++++
****
另一件事是编写相同但不同输出的程序:
Enter height: 3
Enter width: 10
+*+*+*+*+*
+*+*+*+*+*
+*+*+*+*+*
提前感谢那些愿意帮助的人。
] 1
答案 0 :(得分:0)
您可以使用i % 2 == 0
答案 1 :(得分:0)
首先解决方案, 将此代码放在内部循环
中if(i%2==1)
System.out.print("+");
else
System.out.print("*");
对于第二种解决方案: 使用上面的代码只需将'if(i%2 == 1)'替换为'if(j%2 == 1)'