想搭一個雲服務器,開始學Java。
賦值運算符:+=
class Demo6
{
public static void main(String[] args)
{
int i = 10; // 把10賦予給i變量.
i+=2; // i = i+2;
System.out.println("i = "+i);
byte b1 = 1;
byte b2 = 2;
b2+=b1;//b2=b2+b1;報錯,需要強制類型轉換b2=(byte)(b2+b1);b2+=b1在編譯的時候,java編譯器會進行強制類型轉換,不需要我們手動轉換了.
System.out.println("b2:"+ b2);
}
}
取出一個二進制數據的指定位數。要求讀取該二進制數據的低4位
00000000-00000000-00010100-01001101
& 00000000-00000000-00000000-00001111
------------------------------------------------------------------
00000000-00000000-00000000-00001101
class Demo10 {
public static void main(String[] args)
{
int a = 3;
int b = 5;
a = a^b; // a = 3^5
b = a^b; // b = (3^5)^5 = 3
a = a^b; // a = (5^3)^3 = 5
System.out.println("a = "+ a+" b="+b);
}
}
移位運算符:
<<(左移)
規律:一個操作數進行左移運算的時候,結果就是等於操作數乘以2的n次方,n就是左移 的位數.
3<<1 = 3 *2(1) = 6;
3<<2 = 3*2(2) = 12
3<<3 = 3*2(3) = 24
>>(右移)
規律:一個操作數在做右移運算的時候,實際上就是等於該操作數除以2的n次方,n就是右移的位數。
3>>1 = 3 / 2(1) = 1
3>>2 = 3 / 2(2) = 0 。。
>>>(無符號右移) :無符號右移與右移的區別:進行右移運算的時候,如果操作數是一個正數,那麼左邊的空缺位使用0補,如果操作數是一個負數,那麼左邊的空缺位使用1補。而使用無符號右移的時候,不管是正數還是負數都統一使用0補。
/*
使用異或對圖片數據進行加密.
*/
import java.io.*;
class ImageTest
{
public static void main(String[] args) throws Exception
{
//找到圖片文件
File inFile = new File("e:\\加密的圖片.jpg");
File outFile = new File("e:\\解密的圖片.jpg");
//建立數據通道,讓圖片的二進制數據流入
FileInputStream input = new FileInputStream(inFile);
FileOutputStream output = new FileOutputStream(outFile);
//邊讀,把讀到的數據異或一個數據,把把數據寫出
int content = 0; //該變量是用於存儲讀取到的數據
while((content = input.read())!=-1){ // 如果沒有到文件的末尾,那麼繼續讀取數據,讀取到的數據已經存儲到content變量中了。
output.write(content^12);
}
//關閉資源
output.close();
input.close();
}
}
需求: 鍵盤錄入一個分數,根據分數輸出對應的等級。
比如: 100-90 A等級 89-80 B等級 ..... E等級
接受鍵盤錄入數據的步驟:
1. 創建一個掃描器對象。
2. 調用掃描器對象的nextInt方法掃描數據。
3. 導入包。
import java.util.*;
class Demo3
{
public static void main(String[] args)
{
//創建一個掃描器
Scanner scanner = new Scanner(System.in);
//調用掃描器掃描鍵盤錄入的數據
System.out.println("請輸入一個分數:");
int score = scanner.nextInt(); //定義了一個num變量接收掃描到內容。
if(score>=90&&score<=100){
System.out.println("A等級");
}else if(score>=80&&score<=89){
System.out.println("B等級");
}else if(score>=70&&score<=79){
System.out.println("C等級");
}else if(score>=60&&score<=69){
System.out.println("D等級");
}else if(score>=0&&score<=59){
System.out.println("E等級");
}else{
System.out.println("補考..");
}
}
}
import java.util.*;
class Demo8 {
public static void main(String[] args)
{
//創建一個隨機數對象
Random random = new Random();
//調用隨機數對象的nextInt方法產生一個隨機數
int randomNum = random.nextInt(10)+1; //要求隨機數是 1~10
//創建一個掃描器對象
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("請輸入你要猜的數字:");
//調用掃描器的nextInt方法掃描一個數字
int guessNum = scanner.nextInt();
if (guessNum>randomNum){
System.out.println("猜大了..");
}else if(guessNum<randomNum){
System.out.println("猜小了..");
}else{
System.out.println("恭喜你,猜對了`..");
break;
}
}
}
}
需求: 打印一個九九乘法表.
轉義字符:特殊字符使用”\”把其轉化成字符的本身輸出,那麼使用”\”的字符稱作爲轉移字符。
常見的轉義字符有:
\b Backspace (退格鍵)
\t Tab 製表符(製表符的作用就是爲了讓一列對齊) 一個tab一般等於四個空格。
\n 換行
\r 回車 把光標移動到一行的首位置上。
注意: 如果是在windows系統上操作文件的時候需要換行,是需要\r\n一起使用的。
如果是在其他的操作系統上需要換行,僅需要\n即可。
class Demo15
{
public static void main(String[] args)
{
for(int i = 1 ; i<=9 ; i++){
for(int j = 1 ; j<=i ; j++){ //控制列數
System.out.print(i+"*"+j+"="+i*j+"\t");
}
//換行
System.out.println();
}
}
}
class Demo17
{
public static void main(String[] args)
{
aaa:for(int j = 0 ; j<3 ; j++){ // j=0 外層for循環
bbb:for(int i = 0 ; i< 2 ; i++){ // i=0 內層for循環
System.out.println("hello world"); // 1
break aaa;
}
}
}
}