學習Java第二天
賦值運算符:
“=” 是最簡單的基本單元
混合運算符:
+= a+=1 ----> a=a+1
-= b-=1 ----> b=b-1
*= c*=2 ----> c=c*2
/= d/=3 ----> d=d/3
相關練習
public class Demo1 {
public static void main(String[] args) {
int a=12;
System.out.println(a);//12
a+=1;
System.out.println(a);//13
a-=1;
System.out.println(a);12
a*=2;
System.out.println(a);//24
a/=3;
System.out.println(a);//8
a%=3;
System.out.println(a);//2
}
}
比較運算符:
比較兩個數值大小 結果爲true或false
public class Demo2 {
public static void main(String[] args) {
System.out.println(5>3);//true
System.out.println(5>6);//false
}
}
邏輯運算符:
&&代表與 : 當兩邊式子全部爲true,結果才爲true,其中一個爲false,結果爲false
|| 代表或: 其中一個爲true,結果就位true
! 代表非: 本來爲true的話,結果爲false
注意事項: &&與||具有短路功能
&& 左邊爲false時,右邊就不用算了
|| 左邊爲true時,右邊就不用了算了
public class Demo3 {
public static void main(String[] args) {
System.out.println(true && false);//false
System.out.println(true || false);//true
System.out.println(!true);//false
System.out.println(6>3&&6>7);//false
System.out.println(6>3||6>7);//true
}
}
三元運算符:
需要三個數據
格式 : 數據類型 變量名=條件判斷?表達式1:表達式2
分析:在條件判斷中,如果爲真,則輸出表達式1,反之,輸出表達式2
例:
public class Demo4 {
public static void main(String[] args) {
int a=3;
int b=4;
int max=a>b?a:b;
int min=a<b?a:b;
System.out.println(max);//4
System.out.println(min);//3
}
}
判斷語句
if語句
結合上述&&和||,案例如下:
public class Demo5 {
public static void main(String[] args) {
int score=100;
int literary=90;
if ((score>90 && literary>80) ||(score ==100&&literary>70)){
System.out.println("嫁給你");
}
System.out.println("不能嫁給你");
}
}
if…else語句
格式:if(條件表達式){
表達式1
}else{
表達式2
}
例:
public class Demo6 {
public static void main(String[] args) {
int score=99;
if(score>98)
{
System.out.println("老師請吃飯");
}else{
System.out.println("學生請老師吃飯");
}
}
}
多if判斷語句
格式:
if(條件判斷){
//代碼邏輯
}else if{
//代碼邏輯
}else if{
//代碼邏輯
}
…
else{
//代碼邏輯
}
例:
public class Demo7 {
public static void main(String[] args) {
int money=500;
if(money>=500){
System.out.println("買卡迪拉克");
}else if (money>=100&&money<=500){
System.out.println("買奔馳");
}else if (money>=50&&money<=100){
System.out.println("買低配寶馬");
}else if (money>=10&&money<=50){
System.out.println("買現代");
}else{
System.out.println("買桑塔納");
}
}
}
多重if語句
格式:
if(條件判斷){
//代碼邏輯
if(條件判斷){
//代碼邏輯
}else{
//代碼邏輯
}
}
else if(條件判斷){
//代碼邏輯
if(條件判斷){
//代碼邏輯
}else{
//代碼邏輯
}
}
案例:如果今天是週末,我就出去完,如果溫度高於30度,我就去游泳,否則我就去爬山;
如果今天不是週末,我就去工作學習,如果天氣好,我就去找客戶談生意,否則,我就在公司看資料.
public class Demo8 {
public static void main(String[] args) {
int day=6;
int wendu=30;
int weather=1;
if(day==6||day==7) {
System.out.println("我就出去玩");
if(wendu>=30){
System.out.println("我就去游泳");
}else {
System.out.println("我就去爬山");
}
} else if(day>=1&&day<=5){
System.out.println("我就去工作學習");
if (weather == 1) {
System.out.println("我就去找客戶談生意");
} else {
System.out.println("我就在公司看資料");
}
}
}
}
循環結構
for循環
格式:for(初始化表達式1;布爾表達式2;步進表達式3){
//循環體
}
執行規則:
1.計算表達式1,完成必要的初始化工作;
2.判斷表達式2的值,若表達式2的值爲true,則進行3;
3.執行循環體,然後計算表達式3,以便改變循環條件,進行2;
4.結束for語句的執行。
案例:計算100以內偶數和
public class Demo10 {
public static void main(String[] args) {
int s = 0;
int i;
for (i = 1; i <= 100; i++) {
if (i % 2 == 0) {
s = s + i;
}
}
System.out.println(s);
}
}
while循環
格式:while(循環條件){
循環體
}
案例同上:
public class Demo10 {
public static void main(String[] args) {
int s=0;
int i=1;
while(i<=100){
if(i%2==0){
s=s+i;
}i++;
}
System.out.println(s);
}
}