直角三角形(3)
样式:
代码:
Scanner scan=new Scanner(System.in);
System.out.println("请输入想输入三角形的行数");
int length=scan.nextInt();
for(int i=1;i<=length;i++){
for(int n=1;n<i;n++){
System.out.print(" ");
}
for(int j=length-i;j>=0;j--){
System.out.print("*");
}
System.out.println();
直角三角形(4)
样式:
代码:
Scanner scan=new Scanner(System.in);
System.out.println("请输入想输入三角形的行数");
int length=scan.nextInt();
for(int i=1;i<=length;i++){
for(int j=length-i;j>0;j--){
System.out.print(" ");
}
for(int n=1;n<=i;n++){
System.out.print("*");
}
System.out.println();
}
数组练习——冒泡排序
代码:
Scanner scan=new Scanner(System.in);
System.out.println("请确定数据个数:");
int length=scan.nextInt();
int numbers[]=new int[length];
for(int i=0;i<length;i++){
System.out.println("请输入第"+(i+1)+"个数:");
numbers[i]=scan.nextInt();
}
System.out.print("排序前:");
System.out.println(Arrays.toString(numbers));
int max=0;
for(int i=0;i<=numbers.length-1;i++){
for(int j=0;j<numbers.length-i-1;j++){if(numbers[i]>numbers[i+1]){
max=numbers[i];
numbers[i]=numbers[i+1];
numbers[i+1]=max;
}
}
}
System.out.print("排序后:");
System.out.println(Arrays.toString(numbers));
结果: