编写一个Java程序,使用for循环打印正弦波图案的数字直角三角形。
package Shapes3;
import java.util.Scanner;
public class RightTriNumSineWave1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Right Traingle of Numbers in Sine Wave Rows = ");
int rows = sc.nextInt();
System.out.println("Right Traingle of Numbers in Sine Wave format");
for (int i = 1; i <= rows; i++ )
{
System.out.print(i + " ");
int num = i;
for (int j = 1; j < i; j++ )
{
if(j % 2 != 0)
{
System.out.print((num + ((2 * (rows - i + 1)) - 1)) +" ");
num = num + ((2 * (rows - i + 1)) - 1);
}
else
{
System.out.print((num + 2 * (i - j)) +" ");
num = num + 2 * (i - j);
}
}
System.out.println();
}
}
}

这是另一种编写Java程序的常用方法,用于以直角三角形形式显示数字的正弦波图案。
package Shapes3;
import java.util.Scanner;
public class RightTriNumSineWave11 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Right Traingle of Numbers in Sine Wave Rows = ");
int rows = sc.nextInt();
System.out.println("Right Traingle of Numbers in Sine Wave format");
for (int i = 0; i < rows; i++ )
{
for (int j = 0; j <= i; j++ )
{
if(j % 2 == 0)
{
System.out.print(1 + j * rows - (j - 1) * j / 2 + i - j + " ");
}
else
{
System.out.print(1 + j * rows - (j - 1) * j / 2 + rows - i - 1 +" ");
}
}
System.out.println();
}
}
}
Enter Right Traingle of Numbers in Sine Wave Rows = 13
Right Traingle of Numbers in Sine Wave format
1
2 25
3 24 26
4 23 27 46
5 22 28 45 47
6 21 29 44 48 63
7 20 30 43 49 62 64
8 19 31 42 50 61 65 76
9 18 32 41 51 60 66 75 77
10 17 33 40 52 59 67 74 78 85
11 16 34 39 53 58 68 73 79 84 86
12 15 35 38 54 57 69 72 80 83 87 90
13 14 36 37 55 56 70 71 81 82 88 89 91
这个Java示例使用while循环以正弦波格式打印数字的直角三角形图案。
package Shapes3;
import java.util.Scanner;
public class RightTriNumSineWave2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Right Traingle of Numbers in Sine Wave Rows = ");
int rows = sc.nextInt();
System.out.println("Right Traingle of Numbers in Sine Wave format");
int num, j, i = 1;
while(i <= rows)
{
System.out.print(i + " ");
num = i;
j = 1;
while(j < i)
{
if(j % 2 != 0)
{
System.out.print((num + ((2 * (rows - i + 1)) - 1)) +" ");
num = num + ((2 * (rows - i + 1)) - 1);
}
else
{
System.out.print((num + 2 * (i - j)) +" ");
num = num + 2 * (i - j);
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Right Traingle of Numbers in Sine Wave Rows = 15
Right Traingle of Numbers in Sine Wave format
1
2 29
3 28 30
4 27 31 54
5 26 32 53 55
6 25 33 52 56 75
7 24 34 51 57 74 76
8 23 35 50 58 73 77 92
9 22 36 49 59 72 78 91 93
10 21 37 48 60 71 79 90 94 105
11 20 38 47 61 70 80 89 95 104 106
12 19 39 46 62 69 81 88 96 103 107 114
13 18 40 45 63 68 82 87 97 102 108 113 115
14 17 41 44 64 67 83 86 98 101 109 112 116 119
15 16 42 43 65 66 84 85 99 100 110 111 117 118 120