一. 练习题
- 打印这个图案。
**
**
*
public class day4 {
/**
* @author yhl
* @time 2019.03.07
* @content recircle
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=-6;i<=6;i++){
for(int j=0;j<=6-Math.abs(i);j++){
System.out.print("*");
}
System.out.println( );
}
}
}
(备注:如果在打印上下对称的图形时最好使用绝对值的方法还有就是采用从负数到正数的方法;)
- 星型图案
*
public class day4 {
/**
* @author yhl
* @time 2019.03.07
* @content recircle
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<=4;i++){
for(int j=0;j<=4-i;j++){
System.out.print(" ");
}
for(int k=1;k<=2*i+1;k++){
System.out.print("*");
}
System.out.println();
}
}
}
注意其中有三个:一是行二是空格三是*
一.数组
- 定义:具有相同类型数据的集合。
语法:类型[ ] 变量; 或 类型 变量[ ]; - 赋值:变量= new 类型[长度]
例:int[ ] a;
a = new int[10];(定义了一个位整型的长度为10的数组)
合并:int [ ] a = new int[10] ;
赋值合并:int[ ] a={1,3,5,6}; - 下标(索引)
语法:变量[索引] - 常见方法
Length长度 - 数组是一个引用类型。
例1. 定义一个5个元素组成的一维数组,对其赋值,再把该数组反向输出。
public class day4 {
/**
* @author yhl
* @time 2019.03.07
* @content recircle
* @param args
*/
public static void main(String[] args) {
int[] arr={1,2,6,9,8};
for(int i=4;i>=0;i–){
System.out.println(arr[i]);
}
}
}
例2. 定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标
public class day4 {
/**
* @author yhl
* @time 2019.03.07
* @content recircle
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner Sc=new Scanner(System.in);
int a=Sc.nextInt();
int[] arr=new int[10];
for(int i=0;i<=9;i++){
arr[i]=i;
System.out.print(arr[i]);
}
}
}
二.二维数组
类型:类型[ ][ ] 变量={{},{},{}}
例:int[ ][ ] a={{1,5,3},{,=2,6,9},{7}};