2020-09-08課堂筆記
- 主知識點
- 1.java程式基本結構
- 2.駝峰命名法
- 3.資料型別
- 4.變數
- 5.資料型別轉換(自動轉換和強制轉換)
- 6.字串注意事項
- 7.集合(list)與陣列
- 8.分支結構
- 9.回圈結構
- 10.主動拋例外
- 11.主函式main(args)回呼方法
- 12.continue與break
- 專案案例(2個)
主知識點
1.java程式基本結構

注:
1.package行可以有0個或者多個,一般在程式的最前面
2.import行可以有0個或者多個,一般在包之后,類名之前
3.主程式入口引數String[] args 與String args[]具有同等效應,推薦使用前者,方便理解
2.駝峰命名法
一般用于定義變數,類名、屬性、命名空間等,除第一個單詞小寫外,其余單詞首字母大寫
如myFirstProject
3.資料型別
包括基本資料型別8種和參考資料型別
下表列出了8種基本資料型別和對應的默認值
| 型別 | 默認值 |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| boolean | false |
| char | ‘u0000’ |
4.變數
1.區域變數
2.全域變數
3.引數變數
注:區域變數包含引數變數,但二者具有細微差別
引數變數無需定義,區域變數需要先定義
5.資料型別轉換(自動轉換和強制轉換)
在Java中由于繼承和向上轉型,子類可以非常自然地轉換成父類,但是父類轉換成子類則需要強制轉換,
可以從存盤范圍大的轉換到存盤范圍小的;
byte<-short<-int<-long<-float<-double
格式:type b=(type)a (type代表型別,參考上表基本資料型別)
注:可能造成精度丟失!!需謹慎!
6.字串注意事項
1.字串拼接將其他字符自動轉換成字串型別
如下列代碼塊,運行結果是Hello JavaEE51
而不是Hello JavaEE6
public class test
{
public static void main(String[] args)
{
System.out.println("Hello JavaEE"+5+1);
}
}
只有當格式為下列代碼塊時,才能列印出6
public class test
{
public static void main(String[] args)
{
Ststem.out.println("Hello JavaEE"+(5+1));
}
}
字串拼接時需要注意,所有都會變成string型別,如有需求,應添加小括號,來保證程式的正確性
7.集合(list)與陣列
1.list集合的創建與追加、洗掉
List<String> list=new ArraryList<String>();
list.add("hello java");
list.remove(XXX);//xxx可以填充索引,也可以填充Object o(內容)
2.一維陣列的兩種創建
int array[]=new arrary[length];
String array[]={"X","XX","XXX","XXXX"};
8.分支結構
分支結構包括if單路分支,if-else、if-else-if、switch-case、三目運算
9.回圈結構
回圈結構包括while、do-while、for、for-each
注:
1.do-while回圈的回圈體至少執行一次
2.for-each比for性能更高!
如下利用for-each回圈遍歷list集合
for(String i:list)
{
system.out.println(i);
}
10.主動拋例外
Java的例外捕獲機制允許用戶自定義例外,也允許用戶主動拋例外
以下表格列出常見9種例外及其含義
| 例外 | 含義 |
|---|---|
| ClassCastException | 型別轉換例外 |
| ClassNotFoundException | 類未找到例外 |
| ArithmeticException | 算數例外 |
| ArrayStoreException | 陣列中包含不兼容的值例外 |
| ArrayIndexOutOfBoundsException | 陣列下標越界例外 |
| SQLException | 操作資料庫例外類 |
| NullPointerExxeption | 空指標例外 |
| NumberFormatException | 字串轉數字拋出例外 |
| FileNotFoundException | 檔案未找到例外 |
11.主函式main(args)回呼方法
常用于選單,重進主函式
12.continue與break
continue:結束本次回圈,進入下一次回圈
break:結束回圈
專案案例(2個)
1.列印實心、空心等腰三角形(且美觀)

代碼邏輯:雙層回圈,先打所有空格,然后在打*
1.實心
for(int i=0;i<n;i++)
{
for(int j=4;j>i;j--)
{
System.out.print(" ");//列印空格
}
for(int j=0;j<i*2+i;j++)
{
System.out.print("*");
}
System.out.println("\n");
}
2.空心
public class Test {
public static void main(String[] args) {
//控制行數
for(int i=0;i<5;i++)
{
//控制每行的空格數
//總行數-當前行數=每行的空格數
for(int j=4;j>i;j--)
{
System.out.print(" ");
}
// 列印每行星星數
for(int j=0;j<i*2+1;j++)
{
//判斷是否打到最后一行
//1.全部要打 2.美觀
if(i==4)
{
//奇數列印星星 偶數不列印星星
if(j%2==1)
{
System.out.print(" ");
continue;
}
System.out.print("*");
continue;
}
//空心 每行的第一個星星與最后一個星星列印,其余打空格
if(j==0){
System.out.print("*");
continue;
}
//最后一個
if(j==i*2)
{
System.out.print("*");
continue;
}
//其余的都打空格
System.out.print(" ");
}
//換行
System.out.println();
}
}
2.找出所有xx以內能被7整除的數(高性能,滿足要求的代碼)
public class test4 {
public static void main(String args[]) throws IOException
{
int n=0;
System.out.println("請輸入一個整數:");
Scanner s=new Scanner(System.in);
String ss=s.nextLine();
for (int i = 0; i < ss.length(); i++) {
if (! (ss.charAt(i) >= '0' && ss.charAt(i) <= '9')) {
System.out.println("請輸入合理的整數");
//break;
main(args);
}
}
n=Integer.parseInt(ss);
// System.out.println(n);
s(n);
}
public static void s(int n) throws IOException
{
int count=0;
int sum=0;
int[] m =new int[100];
for(int i=1;i<n;i++)
{
if(i%7==0)
{
count++;
// System.out.println(i);
m[count-1]=i;
}
}
System.out.println("符合條件的總個數為:"+count+"分別是:");
for(int i=0;i<count;i++)
{
System.out.println(m[i]);
sum+=m[i];
}
double sum2=sum;
System.out.println("他們的總和為:"+sum);
System.out.println("他們的平均數為:"+sum2/count);
System.out.println("其中最小值為:"+m[0]);
System.out.println("其中最大值為:"+m[count-1]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/38181.html
標籤:其他
上一篇:58. 最后一個單詞的長度
下一篇:Linux 之 條件變數
