Java print example of sine curve

  • 2020-04-01 03:12:01
  • OfStack



package hundred;
import java.lang.Math;
public class SinTest {
    public static void main(String[] args){
     //Y is the column direction, from 1 to -1, and the step size is 0.1
     for (double y = 1;y>=-1;y-=0.1){   
        //Calculate the radians corresponding to y and multiply by 10 to enlarge the figure
        int m = -(int)(Math.asin(y)*10);
           if (y > 0){ 
              for (int x = 1;x <1-m;x++){
               System.out.print(" ");
              }  
                  System.out.print("*");
              //31 is an integer part of 10*, which makes the printed curve more pleasing
              for (int x =1;x <31+2*m;x++){
                  System.out.print(" ");
                 }   
                  System.out.println("*"); 
        } 
           if (y <= 0){    
                 for (int x = 1;x < 32+m;x++){
                  System.out.print(" ");
                 }  
                     System.out.print("*");
                 //31 is an integer part of 10*, which makes the printed curve more pleasing
                 for (int x = 1;x < 31-2*m;x++){
                  System.out.print(" ");
                 }  
                     System.out.println("*");
            }
         }
    }
}



Related articles: