代码:
练习一:打印金字塔
package com.ClassLianxi;
import java.util.Scanner;
public class SanJiaoXing {
public void setSan(){
Scanner input=new Scanner(System.in);
System.out.print("请输入三角形的高:");
int floor=input.nextInt();
int i,j,k;
for(i=1;i<=floor;i++)
{
for(k=1;k<=floor-i;k++)
{
System.out.print(" ");
}
for(j=1;j<=i*2-1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void main(String args[]) {
SanJiaoXing s=new SanJiaoXing();
s.setSan();
}
}
练习二:打印乘法表
package com.ClassLianxi;
import java.util.Scanner;
public class ChengFaBiao {
public void getCheng() {
Scanner input=new Scanner(System.in);
System.out.print("请输入您想打印的乘法表层数:");
int shu=input.nextInt();
for (int e = 1; e<=shu; e++)
{
for (int n = 1; n <=e; n++)
{
System.out.print(n+"*"+e+"="+(e*n)+"\t");
}
System.out.println();
}
}
public static void main(String args[]) {
ChengFaBiao c=new ChengFaBiao();
c.getCheng();
}
}
import java.util.Scanner;
public class ChengFaBiao {
public void getCheng() {
Scanner input=new Scanner(System.in);
System.out.print("请输入您想打印的乘法表层数:");
int shu=input.nextInt();
for (int e = 1; e<=shu; e++)
{
for (int n = 1; n <=e; n++)
{
System.out.print(n+"*"+e+"="+(e*n)+"\t");
}
System.out.println();
}
}
public static void main(String args[]) {
ChengFaBiao c=new ChengFaBiao();
c.getCheng();
}
}
练习三:二维数组的转换
package com.ClassLianxi;
import java.util.Scanner;
public class ShuZu {
public void setShuzu() {
Scanner input=new Scanner(System.in);
System.out.println("请输入数组的阶层数:");
int n=input.nextInt();
System.out.println("请输入数组的数值:");
int array[][]=new int [n][n];
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
array[i][j]=input.nextInt();
}
}
System.out.println("您输入的数组为:");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
System.out.print(array[i][j]+"\t");
if(j==n-1)
System.out.println();
}
}
System.out.println("转换后的数组为:");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
System.out.print(array[j][i]+"\t");
if(j==n-1)
System.out.println();
}
}
}
public static void main(String args[]) {
ShuZu s=new ShuZu();
s.setShuzu();
}
}
import java.util.Scanner;
public class ShuZu {
public void setShuzu() {
Scanner input=new Scanner(System.in);
System.out.println("请输入数组的阶层数:");
int n=input.nextInt();
System.out.println("请输入数组的数值:");
int array[][]=new int [n][n];
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
array[i][j]=input.nextInt();
}
}
System.out.println("您输入的数组为:");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
System.out.print(array[i][j]+"\t");
if(j==n-1)
System.out.println();
}
}
System.out.println("转换后的数组为:");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
System.out.print(array[j][i]+"\t");
if(j==n-1)
System.out.println();
}
}
}
public static void main(String args[]) {
ShuZu s=new ShuZu();
s.setShuzu();
}
}
运行结果:
![](https://i-blog.csdnimg.cn/blog_migrate/ff4aa8baa4d37907f659cac8bb472270.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3217a8bc7fa1f453a1a991ee1a0642ca.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7421ed59ce898e5543bea50229e0c469.png)