Java 基础练习:控制台画圆,画等腰三角,乘法表
画圆
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190830200453442.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3JlbndlaXlpMTQ4Nw==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/201908302007367.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3JlbndlaXlpMTQ4Nw==,size_16,color_FFFFFF,t_70)
代码
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");
}
}
}
画等腰三角形
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019083020094091.png)
代码
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");
}
}
}
乘法表
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190830201015604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3JlbndlaXlpMTQ4Nw==,size_16,color_FFFFFF,t_70)
代码
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");
}
}
}