Java 基础练习:控制台画圆,画等腰三角,乘法表
画圆
效果
代码
package FourPointSeven;
public class Circular {
public static void main(String[] args) {
paintCircular(6);
}
private static void paintCircular(int R){
int x1, x2;
int r = R+1;
for (var i = 0;i < r-2; i++){
x1 = (r - (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
x2 = (2 * (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
for (var j = 0; j <= x1;j++){
System.out.print(" ");
}
System.out.print("*");
for (var k = 0; k < x2;k++){
System.out.print(" ");
}
System.out.print("*");
System.out.print("\n");
}
for (var i = r-2;i >= 0; i--){
x1 = (r - (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
x2 = (2 * (int) (Math.sqrt(2*r*i - (int) Math.pow(i,2))));
for (var j = 0; j <= x1;j++){
System.out.print(" ");
}
System.out.print("*");
for (var k = 0; k < x2;k++){
System.out.print(" ");
}
System.out.print("*");
System.out.print("\n");
}
}
}
画等腰三角形
效果
代码
package FourPointSeven;
public class IsoscelesTriangle {
public static void main(String[] args){
paintIsoscelesTriangle(6);
}
private static void paintIsoscelesTriangle(int len){
for (var i = 0; i < (len*2-1); i=i+2) {
for (var j = 0; j < (len*2-1 - i)/2; j++){
System.out.print(" ");
}
for (var k = 0;k <= i;k++){
System.out.print("*");
}
System.out.print("\n");
}
}
}
乘法表
效果
代码
package FourPointSeven;
public class MultiplicationTable {
public static void main(String[] args) {
for (var i = 1; i <= 9; i++) {
for (var j = 1; j <= i;j++){
System.out.printf("%dX%d=%d",i,j,i*j);
if (j < i){
System.out.print(",");
}
}
System.out.print("\n");
}
}
}