The third day

变量分类(局部变量,成员变量,静态变量)

  成员变量

  (也叫实例变量 member variable) 方法外部,类的内部定义的变量。从属于对象,生命周
      期伴随对象始终;

  静态变量

     (类变量static variable):使用static定义。从属于类,生命周期伴随类始终,从类加载到
      卸载。

 局部变量(local variable)

     方法或语句快内部定义的变量,生命周期是从声明位置开始到方法或语句
      块执行完毕为止;

 1              public class TestVariable{
 2  int a ;   //成员变量,成员变量会自动被初始化
 3  static int size;    //静态变量,从属于类
 4                 
 5                 public static void main(String[] args){
 6                     {
 7                         int age;                 
 8                         age= 18;
 9                      }                      
10                         int salary= 3000;      //局部变量
11                         int gao = 13;
12                         System.out.println(gao);
13                         int i;
14                        // int j=i+5;//编译出错,变量i还未被初始化
15                       }
16                       }

 


常量(Constant)so easy

数据类型:

  一:基本数据类型:

  1数值型:(1)整数类型(byte(1字节),short(2字节),int(4字节),long(8字节)
      (2)浮点类型(float(4字节),double(8字节))
                               2字符型:(char (2字节))例:‘a'
                               3布尔型:(boolean(内存中占一位))     只有  true 和 false

  二:引用数据类型(4字节)

  类(class)  接口(interface)   数组

Java 语言整型常量的四种表示形式

        1.十进制整数
        2.八进制整数:要求以0开头   如:015
        3.十六进制数:要求0x或0X开头    如:0x15
        4.二进制数:要求0b或0B开头,  如:0b01110011
  Java语言的整型常数默认为int型,声明long型常量可以后加’I‘或’L‘
转义字符:/n换行 ; /r回车;/d退格

 1  char a= 'i';               //字符用char
 2  String a= "i";            //字符串用string
 3 boolean:
 4  boolean flag;
 5  flag = true;
 6  if(flag){
 7                         //true分支
 8  }else{
 9          //false分支
10  }

 

运算符:
      算术运算符:+ ,-,  *, /,%, ++,--;
      赋值运算符:=
      扩展运算符:+=,-=,*=,/=;
      关系运算符:>,  <,  >=,  <=, ==, !=, inst, anceof
      逻辑运算符:&&,||,!,^
      位运算符:&,|,^,~, <<,>>,>>>
      条件运算符:?:
      字符串连接符:+
整数运算:
  1.如果两个操作数有一个为long,则结果也为long,
  2.没有long时,结果为int。即使操作数全为short,byte,结果也是int.
浮点运算:
  3.如果两个操作数有一个为double,则结果为double。
  4.只有两个操作数都是float,则结果才为float。
取模运算:操作数一般使用整数,结果是“余数”,“余数”符号和左边操作数相同,如:7%3=1,-7%3=-1
,7%-3=1。
 
赋值和赋值扩展运算符:
 +=             a+=b              a=a+b
  -=             a-=b               a=a-b
 *=              a*=b               a=a*b
                /=               a/=b              a=a/b
                %=             a%=b             a=a%b
字符串连接: int  a =3+7;  syso   (a=10)
     String a= 3+"7";   syso  (a=37)不同类型的,+ 作为连接符
条件运算符:(三个方法)

 1  int score =80;
 2                 int x = -100;
 3  String type = score<60?"不及格":"及格";
 4  System.out.println(type);
 5  if(score<60){
 6    System.out.println("不及格");
 7                  }else{
 8       System.out.println("及格");
 9  }
10                   System.out.println(x>0?1:(x==0?0:-1));
11      

 


运算符优先级:其它不需要记,逻辑非>逻辑与>逻辑或、
字符类型自动转化:   只能容量小的向容量大的转换


                 int a= 234;
  long b= 'a';
   double d = b;
                 float f= 2342342l;
                 byte b2= 123;

 


 注意计算大数据时是否会“溢出”。
使用scanner获得键盘的输入:

 1 import java.util.Scanner;
 2  //测试键盘的输入
 3 public class scanner {
 4   public static void main(String[] args) {
 5  Scanner scanner = new Scanner(System.in);
 6  System.out.println("请输入名字");
 7  String name = scanner.nextLine();
 8  System.out.println("请输入你的爱好");
 9  String favor = scanner.next();
10  System.out.println("请输入你的年龄");
11  int age= scanner.nextInt();
12  
13  System.out.println("#######");
14  System.out.println(name);
15  System.out.println(favor);
16  System.out.println("来到地球的天数:"+age); 
17  }
18  }

 

控制语句和现实逻辑的应用

  任何软件和程序,小到一个练习,大到一个操作系统,本质上都是由“变量,选择语句,循环语句”组成  if单选择结构
if-else双选择结构
if-else if-else多选择结构
 if(布尔表达式){语句1
}else if (布尔表达式){语句2
}else if (布尔表达式){语句3
}……else if  (布尔表达式) {语句n}  

 1 public class Test2elseif {
 2   public static void main(String[] args){
 3    int i = (int)(12*Math.random());
 4    System.out.println("学习多少小时"+i+"决定什么样的成就");
 5    if(i<2){
 6     System.out.println("混个温饱");
 7    }else if(i<5){
 8     System.out.println("学有所成");
 9    }else if(i<8){
10     System.out.println("月薪上万");
11    }else{
12     System.out.println("未来不是梦");
13    }
14   }
15 }

 

switch结构
  Math类的使用:1.java.lang包中的Math类提供了一些用于数学计算的方法
           2.Math.random()该方法用于产生一个0到1区间的double类型的随机数,但是不包括1;
                             int i = (int)(6*Math.random());     //产生一个[0,5]之间的随机整数
                 新手雷区:1.如果if语句不写{},则只能作用于后面的第一条语句。
                                 2.强烈建议,任何时候都写上{},即使里面只有一句话!
//测试选择语句

 1 public class Test1 {
 2   public static void main(String[] args) {
 3  double d = Math.random();   //返回(0,1)之间的随机数
 4  System.out.println(d);
 5  
 6  int i=(int)(6*Math.random()+1);
 7  System.out.println(i);
 8  if(i<=3){
 9   System.out.println("小");
10   System.out.println("小小小");
11  }
12  System.out.println("##############");
13  //通过掷3个塞子看看今天的手气如何?
14  int a=(int)(6*Math.random()+1);
15  int b=(int)(6*Math.random()+1);
16  int c=(int)(6*Math.random()+1);
17  int k= a+b+c;
18  if (k>15){
19   System.out.println("手气不错");
20  }
21  if(k>=10&&k<=15){
22   System.out.println("今天手气一般");
23  }
24  if(k<10){
25   System.out.println("今天手气不怎么样");
26  }
27  System.out.println("得了"+k+"分");
28 }
29 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ defines a class DateV3 with the following: private member variables: int year, month, day; Has three constructors and one destructor as follows: The first constructor takes three parameters, int y, int m, int n; The second is the copy constructor that takes a DateV3 object as the parameter; The third is the default constructor that takes no parameter; The destructor takes no parameter. (3) Has overloaded operators: int operator-(DateV3 & oneDate); // return difference in days between the calling object and oneDate DateV3 operator+(int inc); // return a Date object that is inc days later than the calling object DateV3 operator-(int dec); // return a Date object that is dec days earlier than the calling object DateV3 operator++(); // overload the prefix ++ operator DateV3 operator++(int); // overload the postfix ++ operator friend ostream& operator<< (ostream& outputStream, DateV3& theDate); // overload the << operator Test class DateV3 in the main function as follows: Declare and initialize an object to represent today, which should be the date that you work on this assignment.Declare and initialize an object to represent your OWN birthday.Express John’s birthday given John is 5 days older than yours. Create Tom’s birthday by using the copy constructor, assuming Tom has the same birthday as you. Display how many days have passed since your birth, John’s birth, and Tom’s birth, respectively. Create an DateV3 object, someday, by cloning Tom’s birthday. Increment someday by the prefix operator ++ once, and by postfix operator ++ once.Display someday, today, your birthday, John’s birthday, and Tom’s birthday. Declare a DateV3 object to represent 28 February 2024, display it, apply the prefix ++ operator on it, display it again, and apply the postfix ++ operator on it and display it again.
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值