# 程式彙整(預習用) #### 上課預習用 ### 一、基礎架構 **[C++]** ```C++ #include<stdio.h> #include<stdlib.h> int mian() { } ``` **[Java]** ```java public class Main { public static void main(String[] args) { //主要打程式的地方 } } ``` --- ### 二、輸入輸出 **[C++]** 輸出 => `printf("%d\n",a);` 輸入 => ```c++ printf("請輸入整數:(例如 5 )"); scanf("%d",&a); //&為記憶體的存放位址 printf("%d\n",a); ``` **[Java]** 輸出 => `System.out.println(a);` #### 不同格式的輸出(4種) ```java=1 int a=10; double b=5.9; System.out.print("無跳行"); System.out.println("加入跳行"); //加ln為跳行 System.out.printf("a=%d b=%f\n",a,b); //用C++一樣的方法 System.out.println(a); //java特殊輸出法(與VB相同) System.out.println("串接文字用"+b); //文字與數值串接 用+ ``` 輸入 => #### 引用插件工具箱差別 ```java=1 import java.util.*; //類似C語言的#include(全工具箱) //將java.util.* import進來,其中涵蓋Scanner公用程式 import java.util.Scanner; //取工具箱裡的Scanner輸入函數 //跟 #include<stdio.h> 一樣 => 對應到printf和scanf ``` ##### 引用插件: `import java.util.Scanner;`代表只用輸入插件 ##### 程式: ```java System.out.print("請輸入一個整數:(例如 5 )"); Scanner scanner=new Scanner(System.in); //scanner可以改為任何變數 a=scanner.nextInt(); //常見的為keyboard、input System.out.println(a); ``` * scanner 可以自由定義 --- ### 三、變數 [C++]、[Java]一樣 int a=1; 利用final宣告的變數,其值不能再被更改, 如 `final double PI=3.1415926;` --- ### 四、If判斷 [C++]、[Java]一樣 但在java中沒有if_else if_else ```java if() { } else if { } else { } ``` 所以有些TQC題目會用單一if_else寫,但容易太複雜 --- ### 五、迴圈(for) **[C++]、[Java]一樣** * [Java]多一個寫法,迴圈內宣告變數i ```java for(int i=1;i<=10;i++) { System.out.println(i); } ``` --- ### 六、迴圈(while、do_while) **[C++]、[Java]一樣** ```java while() { } ``` ##### 特殊寫法 ```java while(true) //迴圈永遠成立=>會形成[無窮迴圈] { break; //最後要有條件讓他,強制跳出 } ``` --- ### 七、函數、副程式、遞迴(fun) **[C++]** ```c++ int fun(int a,int b) { return a+b; } ``` **[Java]** ##### 第一種寫法: ```java public static int fun(int a,int b) { return a+b; } ``` ##### 第二種寫法: ```java public static void fun(int a,int b) { //不會return東西回去,遞迴呼叫寫法 } ``` --- ###### tags: `JAVA課堂學習` `複習用` `高科大`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up