一、 Java語言概述:
Java是在C語言基礎上衍生出的一門面向對象的高級語言,繼承了C的大部分語法規則。相較於面向過程的C語言,Java考慮的是讓誰來實現而不是怎樣實現,雖然在效率上遠不及C,但優勢在於擁有豐富統一且強大的庫函數,可操作性較強。其次Java繼承了C++的封裝,對於類的定義更加明確,在接口上有更爲嚴格的操作方式。Java被廣泛接受最重要的原因是其強大的跨平臺操作能力,一次編程可在多種平臺上運行。
二、 基本操作(全部由實例展示):
1、 HelloWord:
package day01;
public class HelloWorld
{
public static void main(String[] argc)
{
System.out.println("HelloWorld");//輸出函數調用
}
}
2、 運算符表達式:
包含:與或非運算、三目運算,字符串鏈接、輸入函數
package 運算符和表達式;
import java.util.Scanner;//輸入
public class leap_year
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);//輸入
System.out.println("請輸入年齡:");
int age = scan.nextInt();//接受一個整數
//System.out.println(age);
boolean b = age >= 18 && age <= 50;//並運算,比較運算
System.out.println(b);
System.out.println("請輸入年份:");
int year = scan.nextInt();
boolean flag = year % 4 == 0 && year % 100 != 0 || year %400 ==0;//或運算
String str = flag ? year + "是閏年" : year + "不是閏年"; //三目運算,字符串鏈接
System.out.println(str);//在輸出函數中可進行判斷
scan.close();//結束
}
}
3、 條件表達式:
package 條件表達式;
import java.util.Scanner;
public class IfElse
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
//比較三個數的值
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int temp;
if(a > b)
{
temp = a;
a = b;
b = temp;
}
if(b > c)
{
temp = b;
b = c;
c = temp;
}
if(a > b)
{
temp = a;
a = b;
b = temp;
}
System.out.println(a + "<" + b + "<" + c);
//變量的作用域爲從變量定義到包含他的最近的大括號結束
}
}
4、 循環結構:
package 循環結構;
import java.util.Scanner;
public class GuessNumber
{
//猜數字
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random() * 1000 + 1);
int guess = scan.nextInt();
while(guess != num)
{
if(guess == 0)break;
else if (guess < num)
{
System.out.println("太小了");
guess = scan.nextInt();
}
else
{
System.out.println("太大了");
guess = scan.nextInt();
}
}
if(guess == num)System.out.println("猜對了");
else System.out.println("下次再來玩");
scan.close();
}
}
5、 數組:
包含:定義,初始化,複製,擴容,排序
package 數組;
import java.util.Arrays;
import java.util.Random;//生成隨機數
import java.util.Scanner;
public class arr
{
public static void main(String[] args)
{
int[] arr1;//定義
arr1 = new int[4];//初始化且值默認爲0
int[] arr2 = new int[4]; //定義並初始化
int[] arr3 = {1, 5, 8, 3};//只能聲明並同時初始化
int i = arr1.length;//數組大小
Random rand = new Random();
int num = rand.nextInt(100);//0~99之間的隨機數
for(i = 0; i < arr1.length; i++)
arr1[i] = rand.nextInt(100);
for(i = 0; i < arr1.length; i++)
System.out.println(arr1[i]);
System.out.println("***********我是分割線******");
//數組的複製
System.arraycopy(arr1, 0, arr2, 1, 2);//(從哪一個數組,從哪個元素開始,複製到哪個數組,從哪個位子開始,複製多長)
for(i = 0; i < arr2.length; i++)
System.out.println(arr2[i]);
//數組複製縮2
int[] a1 = Arrays.copyOf(arr1, 8);
for(i = 0; i < a1.length; i++)
System.out.println(a1[i]);
System.out.println("*********我是分割線**********");
//數組擴容
a1 = Arrays.copyOf(a1, a1.length+1);
System.out.println("*********我是分割線**********");
//數組排序
for(i = 0; i < a1.length; i++)
a1[i] = rand.nextInt(100);
Arrays.sort(a1);
for(i = 0; i < a1.length; i++)
System.out.println(a1[i]);
}
}