java基礎第二天

spacer.gif
public class zuoye_4{
public static void main(String args []){
int score = 80;
String s = score < 0?"非法":(score<60?"不及格":(score<70?"及格":(score<80?"中":(score<90?"良":(score<=100?"優秀":"非法"))
)));
System.out.println(s);
}
}
spacer.gif
---------------------
自動輸入
import java.util.*;
public class zuoye_4{
public static void main(String args []){
int score;
Scanner sc = new Scanner(System.in);
score = sc.nextInt();
String s = score < 0?"非法":(score<60?"不及格":(score<70?"及格":(score<80?"中":(score<90?"良":(score<=100?"優秀":"非法"))
)));
System.out.println(s);
}
}
spacer.gif
spacer.gif
複習


本課內容:
1、程序流程控制
a:順序,(找到main方法,之後順序執行)
b:分支,就是在程序執行過程中出現岔路口
例子:
如果不上課,可以happy
否則,在教室上課
如果android學的非常好,工資可以使10K
否則工資可以是6K
c:循環,跑圈,10000米,需要25圈
2、 分支
if 、 switch
if語句格式

條件:boolean類型的表達式
如果條件爲真則進入大括號分支執行
1、如果條件爲真則進入大括號分支執行
if(條件)){
}

2、如果條件爲真則進入大括號分支執行否則進入else執行
if(條件)){
}
else{
}
3
if(條件)){
}
else if (條件){
}
else if (條件){
}
4
if(條件)){
}
else if (條件){
}
else if (條件){
}
。。。。。
else(條件){
}
如果以上都不滿足執行最後一個else條件
switch 語句
swtich (){
    case 後邊跟的是常量 : 分支1;  break;退出
    case 後邊跟的是常量 : 分支1;  break;退出
...........
default: system.out
}
swtich (這裏跟的是變量和變量的表達式)
case 後邊跟的是常量 : 分支1;
case語句沒有break終結 具有穿透性
大月 1、3、5、7、8、
//給定年,月份,判斷該月有多少天
1、判斷閏年:a:能被4整除,但不能被100整除; b:能被400整除;
/*
給定年,月份,判斷該月有多少天
1、判斷閏年:a:能被4整除,但不能被100整除; b:能被400整除;
,1、3、5、7、8、10、12 始終31天
4、6、9始終30天
只有2月會變
import java.util.*;
public class Test01{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.print("請出入年份:");
int year = scan.nextInt();
System.out.println("請出入月份:");
int math = scan.nextInt();
switch (math)
{
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
System.out.println("29天");
}
else
{
System.out.println("28天");
}
switch(month){
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
}
default:
System.out.println("31天");
break;
}
}
}
*/
/*
//給定年,月份,判斷該月有多少天
1、判斷閏年:a:能被4整除,但不能被100整除; b:能被400整除;
*/
import java.util.*;
public class NianYue{
public static void main(String args[]){
int year,month,days;
Scanner i = new Scanner(System.in);
System.out.print("請出入年份:");
year = i.nextInt();
System.out.println("請出入月份:");
month = i.nextInt();
System.out.println("年份爲"+year);
System.out.println("月份爲"+month);
switch (month){
case 2:
if ((year%4==0 && year%100!=0) || (year%400==0)){
System.out.println("29天");
}
else{
System.out.println("28天");
}
}
switch(month){
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
}
default:
System.out.println("31天");
break;
}
}
jdk 1.6中
Swith()  括號裏邊和case後邊可以跟的數據類型:byte、short,int 、char
jdk 1.7
新增可以使用字符串
3、 循環
for , while 、 ,do 。。。。。while
for循環語法格式
for(表達式1;表達式2;表達式3){
       循環體4
}
表達式1:定義循環變量並初始化,常用循環變量 i、j、k
表達式2;循環條件
表達式3:改變循環變量的值
4、生成隨機數 Math
spacer.gif
隨機數Math.random()*100 擴大100倍
spacer.gif
強轉
5、break , 結束循環
continue ,結束本次循環,繼續下一次
總結:
1、分支、
if(){}
else{}
if(){
}
else if (){
}
switch(變量或者變量的表達式) {
case 常量,break;
......
default:
}
2、循環
for循環基本結構上邊寫有
預習 while , do while


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章