計(jì)算機(jī)的一些基本常識(shí)
數(shù)據(jù)單位
在計(jì)算中表示數(shù)據(jù)的最小單位是比特 - bit - 一個(gè)0或者一個(gè)1
- 字節(jié)(Byte) = 8比特(bit)
- 千字節(jié)(KB) = 1024B
- 兆字節(jié)(MB) = 1024KB
- 吉字節(jié)(GB) = 1024MB
- 太字節(jié)(TB) = 1024GB
// 100Mbps ---> 12.5MBps
// 1km ---> 1000m ---> 10^3
// 1KB ---> 1024B ---> 2^10
關(guān)于二進(jìn)制的拓展
// 123 = 1 * 10^2 + 2 * 10 ^ 1 + 3 * 10 ^ 0
// 一個(gè)字節(jié)
// 00000000
// 00000001
// 內(nèi)存中的-2
// 10000010 - 原碼
// 11111101 - 反碼
// 11111110 - 補(bǔ)碼
// 內(nèi)存中的127
// 01111111 = 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 = 2^7 - 1
// 01111111 11111111 = 2^15 - 1
// 01111111 11111111 11111111 11111111 = 2^31 - 1
01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 = 2^63 - 1
變量和常量
// 關(guān)鍵字 - 在程序中有特殊含義的單詞 - keyword
// 表示類型的關(guān)鍵字:
// - byte(1) / short(2) / int(4) / long(8)
// - float(4) / double(8)
// - char(2)
// - boolean(1bit) true(真) / false(假)
// 標(biāo)識(shí)符 - 給變量、常量、方法、類、接口等起的名字就叫標(biāo)識(shí)符 - identifier
// 標(biāo)識(shí)符命名規(guī)則 :
// 1. 字母(Unicode字符)、數(shù)字、下劃線和$, 數(shù)字不能開頭
// 2. 大小寫敏感(區(qū)分大小寫)
// 3. 不能使用關(guān)鍵字作標(biāo)識(shí)符
// 4. 見名知意
// 5. 駝峰命名法
// - 命名類、接口、枚舉、注解:每個(gè)單詞首字母大寫
// - 命名變量、方法:第一個(gè)單詞全小寫從第二個(gè)單詞開始每個(gè)單詞首字母大寫
// - 命名常量用全大寫,多個(gè)單詞用下劃線分隔
// 運(yùn)算符 - operator
// - 賦值運(yùn)算符 =、+=、-=、*=、/=、%=、……
// - 算術(shù)運(yùn)算符 +、-、*、/、%
// - 比較運(yùn)算符 >、<、>=、<=、==、!= - 結(jié)果一定是布爾值
// - 邏輯運(yùn)算符 &(邏輯與運(yùn)算)、|(邏輯或運(yùn)算)
// - 短路運(yùn)算符 &&(短路與運(yùn)算-而且)、||(短路或運(yùn)算-或者)
// 字面量(直接量) - literal
// - 整數(shù)字面量: 123 0x123 0123 0b101 10L
// - 小數(shù)字面量: 1.23F 1.23 1.23e2 1.23e-2
// - 字符字面量: 'a' 'A' '昊' '\n' '\'' '\123'
// - 布爾字面量: true false
// - 字符串字面量: "hello"
// - 引用字面量: null
// - 類型字面量: String.class int.class
// 分隔符 - separator
// - ; , { } ( ) [ ] : 空格
基礎(chǔ)的程序
System.out.println(int.class);
System.out.println(String.class);
int x =3, y = 5, z = 3;
// System.out.println(x > y); // false
// System.out.println(x != y); // true
// System.out.println(x == y); // false
// System.out.println(x >= z); // true
System.out.println(x > y && y > z); // false
System.out.println(x > y || y > z); // true
// final int CAPACITY_OF_ROOM = 120;
// int ageOfStudent;
// double A = 100.1;
int a = 10;
a += 20; // a = a + 20;
a *= 5; // a = a * 5;
a *= a + 2; // a = a * (a + 2);
System.out.println(a);
// int 1a = 1000;
// int public = 12;
// double static = 3.5;
int a1 = 101; // 十進(jìn)制
int a2 = 0101; // 八進(jìn)制
int a3 = 0x101; // 十六進(jìn)制
int a4 = 0b101; // 二進(jìn)制 Java 7+
long a5 = 12345678L; // 長(zhǎng)整數(shù)
int a6 = 12_345_678; // Java 7+
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
System.out.println(a5);
System.out.println(a6);
float b1 = 1.23F; // 單倍精度浮點(diǎn)數(shù)
double b2 = 1.23; // 雙倍精度浮點(diǎn)數(shù)
double b3 = 123.456;
// 科學(xué)計(jì)數(shù)法
double b4 = 1.23456e2;
double b5 = 1.23456e-2;
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
System.out.println(Byte.MIN_VALUE + "~" + Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE + "~" + Short.MAX_VALUE);
System.out.println(Integer.MIN_VALUE + "~" + Integer.MAX_VALUE);
System.out.println(Long.MIN_VALUE + "~" + Long.MAX_VALUE);
System.out.println(Float.MIN_VALUE + "~" + Float.MAX_VALUE);
System.out.println(Double.MIN_VALUE + "~" + Double.MAX_VALUE);
char $x = 'A';
char $y = '昊';
// String name = "王大錘";
System.out.println($x);
System.out.println((int) $x);
System.out.println($y);
System.out.println((int) $y);
System.out.println((char) 26115);
boolean z0 = true;
boolean z1 = false;
System.out.println(z0);
System.out.println(z1);
}
}
練習(xí)
- 個(gè)人所得稅算法
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的五險(xiǎn)一金");
double insurance = input.nextDouble();
System.out.println("請(qǐng)輸入你的工資");
double salary = input.nextDouble();
double t = salary - 3500 - insurance;
double x =0;
double z = 0;
if (t < 0) {
System.out.println("你不需要繳納個(gè)人所得稅");
} else if (t <= 1500) {
z = 0.03;
x = 0;
} else if (t <= 4500) {
z = 0.1;
x = 105;
} else if (t <= 9000) {
z = 0.2;
x = 555;
} else if (t <= 35000) {
z = 0.25;
x = 1005;
} else if (t <= 55000) {
z = 0.3;
x = 2755;
} else if (t <= 80000) {
z = 0.35;
x = 5505;
} else {
z = 0.45;
x = 13505;
}
double tax = t * z - x;
System.out.printf("你需要繳納的個(gè)人所得稅為:%.2f元\n" , tax);
System.out.printf("本月實(shí)發(fā)工資為:%.2f元\n",(salary-tax-insurance));
input.close();
}
- 輸入x年x月,判斷這個(gè)月有多少天
import java.util.Scanner;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入一個(gè)月份");
if (input.hasNextInt()) {
int month = input.nextInt();
if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12) {
System.out.println("這個(gè)月有" + 31 + "天");
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
System.out.println("這個(gè)月有" + 30 + "天");
}
if (month == 2) {
System.out.println("請(qǐng)輸入一個(gè)年份");
int year =input.nextInt();
if ((year % 4 == 0 && year % 100 != 0)
|| year % 400 == 0) {
System.out.println("這個(gè)月有" + 29 + "天");
}
else {
System.out.println("這個(gè)月有" + 28 + "天");
}
}
} else {
System.out.println("這里需要一個(gè)整數(shù)??!");
}
// System.out.println("請(qǐng)輸入一個(gè)年份");
input.close();
}
- 三角形成立的條件和周長(zhǎng)面積
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入三條邊長(zhǎng)");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
boolean isvalid = (a + b) > c && (a + c) > b && (b + c) > a;
if (a > 0 && b > 0 && c > 0 && isvalid) {
double p = a + b + c;
double S = Math.sqrt(p / 2 * (p / 2 - a) * (p / 2 - b) * (p / 2 - c));
System.out.printf("三角形的周長(zhǎng)是:%.2f\n", p);
System.out.printf("三角形的面積是:%.2f\n", S);
} else {
System.out.println("不能構(gòu)成三角形!");
}
input.close();
}
4.分段函數(shù)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入x=");
double x = input.nextDouble();
double y;
if (x < -1) {
y = 3 * x + 5;
} else if (x <= 1) {
y = x - 1;
} else {
y = 5 * x - 3;
}
System.out.println("y =" + y);
input.close();
}
5.潤(rùn)年的判斷
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// int a = input.nextInt();
// int b = input.nextInt();
// System.out.println(a>b?a:b);
System.out.println("請(qǐng)輸入一個(gè)年份");
//分支結(jié)構(gòu)(選擇結(jié)構(gòu)) - 可以讓程序有多條執(zhí)行路徑
if (input.hasNextInt()){
int year = input.nextInt();
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)
|| year % 400 == 0;
System.out.println(year + "年" +
(isLeapYear ? "是" : "不是") + "潤(rùn)年");
input.close();
}
else {
System.out.println("瓜兮兮的,這里需要一個(gè)整數(shù)!!");
}
// 注釋:上題是判斷是否是潤(rùn)年。
}
6.Graps賭博游戲
游戲規(guī)則:
//-兩顆篩子
//-第一次:
//玩家搖出 7或 11 玩家勝
// 2 3 或 12 莊家勝
// 其他點(diǎn)數(shù) 游戲繼續(xù)
//玩家再搖
// 如果搖出了7點(diǎn) 莊家勝
// 如果搖出了第一次的點(diǎn)數(shù) 玩家勝
// 其他情況 游戲繼續(xù)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)按Enter開始游戲");
String x = input.nextLine();
int a =(int) (Math.random()*6+1);
int b =(int) (Math.random()*6+1);
int num = a + b;
int n = 1;
System.out.println("第1次你搖出了" + a + "和" + b );
System.out.println("總計(jì)" + num + "點(diǎn)");
if (num == 7 || num == 11){
System.out.println("第1局玩家獲勝");
}
else if (num == 2 || num == 3 || num == 12) {
System.out.println("第1局莊家獲勝");
}
else {
while(true){
System.out.println("現(xiàn)在平局,請(qǐng)按Enter繼續(xù)");
String y = input.nextLine();
int c =(int) (Math.random()*6+1);
int d =(int) (Math.random()*6+1);
int newnum = c + d;
n+=1;
System.out.println("第" + n + "次你搖出了" + c + "和" + d );
System.out.println("總計(jì)" + newnum + "點(diǎn)");
if (newnum == 7){
System.out.println("莊家獲勝");
break;
}
else if (newnum == num) {
System.out.println("和第一輪總和相同,玩家獲勝");
break;
}
}
}
input.close();
}
7.捕魚問題:
n個(gè)人,第1個(gè)人先將魚扔掉一只然后平分五份拿走其中一份,第2個(gè)人也將魚扔掉一只然后平分五份拿走其中一份,以此類推,直到第n個(gè)人的時(shí)候也扔掉一只然后平分五份拿走其中一份,求這堆魚至少有多少只。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入捕魚的人數(shù)");
int n = input.nextInt();
for (int a = 1; a <= Integer.MAX_VALUE; a++) {
int i = 1;
int b = a;
boolean x = true;
while (i <= n && x) {
if (b % n == 1) {
b = (n - 1) * (b - 1) / n;
i++;
x = true;
} else {
i++;
x = false;
}
}
if (x && i==(n+1)) {
System.out.print(a);
break;
}
}
input.close();
}