首頁 > 軟體

你知道怎麼從Python角度學習Java基礎

2022-02-17 13:01:22

1. 變數

賦值

專案JavaPythonJavaScriptVBA
必須先宣告
宣告int x;dim x%
賦值x=1;x=1x=1x=1
宣告並賦值int x=1;x=1x=1
nullNonenull undefinedNull

資料型別

專案JavaPythonJavaScriptVBA
整數int x=1;x=1x=1x=1
字元char a='A';
字串String a="A";a="A"
a='A'
a="A"
a='A'
a="A"
小數float f=3.14f;
double d=1.7d
f=3.14f=3.14f=3.14
布林boolean b=true;b=Trueb=trueb=True
常數final double PI=3.14;PI=3.14const PI=3.14Const PI=3.14
物件StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
sb = ShaBi()sb = new Shabi()x = CreateObject("Scripting.Dictionary")
型別轉換只允許向上轉換允許允許允許

2. 符號

計算運運算元

運運算元JavaPythonJavaScriptVBA
++++
----
****
////
求餘%%%mod
次冪3**23**2
自增++++
自減----
疊加+=+=+=
疊減-=-=-=
疊乘*=*=*=
疊除/=/=/=
括號()()()()
字串連線++++

比較運運算元

運運算元JavaPythonJavaScriptVBA
大於>>>>
大於等於>=>=>=>=
小於<<<<
小於等於<=<=<=<=
等於========
不等於!=!=!=!=
and&&and&&and
or||or||or
not!not!not

程式碼符

符號JavaPythonJavaScriptVBA
跳脫符“”
換行符;:;:
換行符是否可省略不可省略大部分可省略大部分可省略

註釋

符號JavaPythonJavaScriptVBA
單行註釋//#//
多行註釋/*…*/“”"…"""
’’’…’’’
/*…*/

文字元

符號JavaPythonJavaScriptVBA
單行字元"
"
"
單行字串""
"
"
多行字串“”"…"""“”"…"""
’’’…’’’

3. if

一行if

// Javax = a > b ? c : d;
# Python
x = c if a > b else d
// JavaScript
x = a > b ? c : d
' VBA
if a > b Then x = c Else x = d

一次判斷

// Java
if (a > b) {
	x = c;
} else {
	x = d;
}
# Python
if a > b:
	x = c
else:
	x = d
// JavaScript
if (a > b) {
	x = c
} else {
	x = d
}
' VBA
If a > b Then
	x = c
Else
	x = d
End If

多次判斷

// Java
if (a > b) {
	x = c;
} else if (a > bb) {
	x = cc;
} else {
	x = d;
}
# Python
if a > b:
	x = c
elif a > bb:
	x = cc
else:
	x = d
// JavaScript
if (a > b) {
	x = c
} else if (a > bb) {
	x = cc
} else {
	x = d
}
' VBA
If a > b Then
	x = c
ElseIf a > bb Then
	x = cc
Else
	x = d
End If

4. for

下標迴圈

// Java
for (int i=0;i<100;i++) {	
	System.out.println(i);
}
# Python
for i in range(100):
	print(i)
// JavaScript
for (var i=0;i<100;i++) {	
	console.log(i)
}
' VBA
For i = 1 to 100 step 1
	Debug.Print i
next

陣列遍歷迴圈

// Java
for (int a:arr) {
	System.out.print(a);
}
# Python
for a in arr:
	print(a)
// JavaScript
for (a in arr) {
	console.log(a)
}
' VBA 
For Each a in arr
	Debug.Print a
Next
專案JavaPythonJavaScriptVBA
中斷迴圈breakbreakbreakExit For
跳過迴圈continuecontinuecontinuegoto

5. while

// Java
int i;
while (i < 100) {
	System.out.println(i);
	i++;
}
// java的另一個while
int i;
do {
	System.out.println(i);
	i++;
} while (i < 99);
# Python
i = 0
while True:
	if i < 100:
		print(i)
	else:
		break
// JavaScript
i = 0
while (i < 100) {
	console.log(i)
	i++
}
' VBA
' 1
i = 0;
While i < 100
	Debug.Print(i)
Wend
' VBA
' 2
i = 0;
Do While i < 100
	Debug.Print(i)
Loop
' VBA
' 3
i = 0;
Do 
	Debug.Print(i)
Loop While i < 99
' VBA
' 4
i = 0;
Do Until i >= 100
	Debug.Print(i)
Loop
' VBA
' 5
i = 0;
Do
	Debug.Print(i)
Loop Until i >= 99
專案JavaPythonJavaScriptVBA
中斷迴圈breakbreakbreakExit For
跳過迴圈continuecontinuecontinuegoto

6. 陣列

專案JavaPythonJavaScriptVBA
定義int[] x = {1,2,3,4,5};x = [1,2,3,4,5]x = [1,2,3,4,5]dim Arr()
符號{}[]
{}
()
[]Array()
索引x[0];x[0]x[0]Arr(0)
型別混用不允許x=[1,'a']x=[1,'a']Arr=Array(1,"a")
不允許x.append('b')
x.insert(0,'c')
x.push('b')Redim Preserve Arr(4)
Arr(4) = 3
不允許x.pop(1)
del x[1]
x.pop(1)Redim Arr(1)
x[0] = 6;x[0] = 6x[0] = 6Arr(0)=6

7. 程式結構

Java

/**
* 檔案註釋
*/
public class Hello {
	public static void main(String[] args) {
		// 主程式說明
		userFunction usf = new userFunction();
		usf.setArg("Hello"); 
		System.out.println(usf.getArg());
		/* 多行註釋
		分行 */
	}
}	

class userFunction {
	private String arg;
	
	public void setArg(String arg) {
		// 設定
		this.arg = arg;
	}
	
	public String getArg() {
		// 返回
		return this.arg;
	}	
}

Python

'''
檔案說明
'''

class userFunction:
	def __init__(self):
		pass
		
	def setArg(self,arg):
		self.arg = arg
	
	def getArg(self):
		return self.arg

if __name__ == '__main__':
	usf = userFunction()
	usf.setArg("Hello")
	print(usf.getArg())

JavaScript

function userFunction(args) {
	x = process(args)
	return x
}

VBA

Sub userSub()
	x = userFunction(args)
	Debug.Print x
End Sub

Function userFunction(args) as String
	userFunction = process(args)
End Function

8. 輸入輸出

輸出

專案JavaPythonJavaScriptVBA
輸出System.out.println
System.out.print
printconsole.logDebug.Print
格式化輸出System.out.printf
System.out.format
format
快速格式化f'{d} is a number'`${d} is a number`

輸入

專案JavaPythonJavaScriptVBA
輸入import java.util.Scanner

Scanner scanner = new Scanner(System.int);
String ipt = scanner.nextLine();
ipt = input('請輸入:')var ipt = prompt('請輸入','預設值')ipt = InputBox("請輸入",,"預設值")

9. 異常捕獲

專案JavaPythonJavaScriptVBA
異常捕獲try {..}
catch {...}
finally {...}
try:
except:
finally:
try {..}
catch {...}
finally {...}
On error goto tag

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!          


IT145.com E-mail:sddin#qq.com