public class ArrayDemo01 {
public static void main(String[] args) {
//声明数组
int[] arr = new int[5];
System.out.println(arr[0]);//byte,short,int,long初始值为0
double[] drr = new double[5];
System.out.println(drr[0]);//float,double初始值为0.0
boolean[] brr = new boolean[5];
System.out.println(brr[0]);//boolean初始值为flase
char[] crr = new char[5];
System.out.println(crr[0] + "c");//char初始值为空字符
}
}
public class ArrayDemo02 {
public static void main(String[] args) {
int[] arr = new int[] {1,2,3,4,5};
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
//System.out.println(arr[5]); ArrayIndexOutOfBoundsException
//数组遍历
for (int i = 0; i < arr.length; i++) {//arr.length.fori
System.out.println(arr[i]);
}
}
}
import java.util.Random;
public class DoubleColorBall {
public static void main(String[] args) {
//声明一个int类型的 长度为7的一维数组 动态初始化
int[] arr = new int[7];
//创建一个随机数对象
Random random = new Random();
//开始摇号(像数组中添加值)
//添加红球(1-33)
for (int i = 0; i < arr.length - 1; i++) {
//获取随机数(1-33)并赋值给[]arr
arr[i] = random.nextInt(33) + 1;
//去重
for (int j = i - 1; j >= 0 ; j--) {
if (arr[i] == arr[j]){//号码重复
i--;//重新摇号
break;
}
}
}
//添加蓝球(1-17)
arr[arr.length - 1] = random.nextInt(17) + 1;
//打乱顺序
int num,a,c;
num = arr[arr.length - 1];
a = random.nextInt(arr.length - 1) ;
c = a + 1;
arr[arr.length - 1] = arr[a];
arr[a] = num;
System.out.println("其中第" + c + "个为蓝球");
System.out.print("本期中奖结果为:");
//遍历数组
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
//System.out.println(a);
}
}
public class Point {
//创建成员变量
int x;
int y;
//当用户没有写任何的构造方法是,系统会自动为程序提供一个无参构造方法
/*
public Point() {
System.out.println("无参构造正在被调用");
}
**/
//如果写了构造方法后(无论是有参还是无参)系统都不会再提供无参构造方法
public Point() {//无参构造
}
public Point(int x, int y) {//全参构造
this.x = x;
this.y = y;
}
public void print(){
System.out.println("x = " + x + " y = " +y);
}
public void setX(int x) {
this.x = x;
}
public void addX() {
this.x = x + 1;
}
public void setY(int y) {
this.y = y;
}
public void Yadd(int z) {
this.y = y + z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
//创建成员方法
public static void main(String[] args) {//main方法是JVM在调用(JAVA虚拟机)
/*
//声明一个point类型的引用point,指向point类型的对象
Point point = new Point();
System.out.println("x = " + point.x);
System.out.println("y = " + point.y);
//对成员变量进行赋值
point.x = 3;
point.y = 4;
System.out.println("x = " + point.x);
System.out.println("y = " + point.y);
**/
Point p1 = new Point();
Point p2 = new Point(1,2);
p1.print();
p2.print();
p1.addX();
p1.Yadd(9);
p1.print();
}
}
public class Phone {
//创建成员变量
String brand;
double price;
//创建成员方法
public void show(){
System.out.println("品牌:" + brand + ",价格:" + price);
}
public static void main(String[] args) {
声明Phone类型的引用指向Phone类型的对象p
Phone p = new Phone();
//调用成员方法
p.show();
//对成员变量进行赋值
p.brand = "nokia";
p.price = 598.5;
//调用成员方法
p.show();
}
}
public class MethodDemo01 {
//定义一个成员方法计算a+b
public int add(int a , int b){//形式参数
return a + b;//返回a+b的值
}
public static void main(String[] args) {
声明一个MethodDemo01类型的引用m,指向MethodDemo01类型的对象
MethodDemo01 m = new MethodDemo01();
int sum = m.add(1,2);//实际参数(即对形参进行初始化操作)类型,顺序,个数要与形参对应
//打印输出sum的值
System.out.println("sum = " + sum);
}
}
public class Vehicle {
double speed;
String type;
public void move(){
}
public void setSpeed(double speed) {
this.speed = speed;
}
public void setType(String type) {
this.type = type;
}
public double speed() {
return speed;
}
public String type() {
return type;
}
public static void main(String[] args) {
Vehicle v =new Vehicle();
v.setSpeed(10);
v.setType("宝马");
System.out.println("车的类型为" + v.type() + "车的速度为" + v.speed());
}
}
public class Girl {
//定义成员变量
String name;//姓名
int age;//年龄
boolean bf;//是否有男朋友
//定义成员方法对成员变量进行打印`
public void print(){
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("是否有男朋友:" + bf);
}
public static void main(String[] args) {
//声明一个Girl类型的引用g,指向Girl类型的对象
Girl g = new Girl();
//直接调用成员方法print
g.print();//打印出的为初始值
//对成员变量进行赋值
g.name = "貂蝉";
g.age = 18;
g.bf = true;
//再次调用成员方法
g.print();//打印出的为赋值后的结果
}
}