顯示具有 JAVA-流程控制類 標籤的文章。 顯示所有文章
顯示具有 JAVA-流程控制類 標籤的文章。 顯示所有文章

2013年2月26日 星期二

[JAVA] java return 回傳/返回 語句介紹用法



return 語法有二種格式,其用意在結束該方法(退出方法)

    一、 return expression:
在return 後加入回傳變數。
必需在方法前宣告回傳的型態,在return 後的型態要相同。
例:
宣告回傳型態為int
在return 後也要是int的型態
public int testReturn1(int pNum) {
int returnData = pNum + 1 + 2 + 3;
return returnData;
}

    二、return: 此用法為沒有回傳值的意思,必需在方法前宣告為void。

public void testReturn2(int pNum) {
// 傳入值為0,離開此方法。
if (0 == pNum) return;
// 傳入值不為0,則做運算。
System.out.println(pNum / pNum);
// 你在方法的最後不加retrun 也是會結束此方法,返回上一層的呼叫此方法的程式。
// 預設在方法的最後都會有 return;
}




public class TestReturn {

 public static void main(String[] args) {
  System.out.println("Test 1 ...");
  System.out.println(testReturn1(5));
  System.out.println("Test 2 ...");
  testReturn2(0);
  System.out.println("Test 3 ...");
  testReturn2(5);
 }

 public static int testReturn1(int pNum) {
  int returnData = pNum + 1 + 2 + 3;
  return returnData;
 }

 public static void testReturn2(int pNum) {
  // 但傳入值為0,離開此方法。
  if (0 == pNum)
   return;
  //
  System.out.println(pNum / pNum);
  // 預設在方法的最後都會有 return;
 }
 
}


返回[JAVA]流程控制語法目錄
















其它文章

[JAVA]使用try-catch-finally基本用法



使用方法
try{
//這區塊為可能發生例外的敘述
}
catch(例外型態 變數名稱)
{
  //這區塊為例外發生的處理
}
finally {
    //這區塊為最後的處理
}






public class testException {
 public static void main(String[] args) {
  //基本型
  int x = 5, y = 0;
  //有發生Exception
  try {
   int result = x / y;
   System.out.println("結果=" + result);
  }
  catch (Exception e) {
   System.out.println("分母不可為0");
  }
  finally {
   //這區塊為最後的處理 ,可有可無。
   System.out.println("測試一結束");
  }
  //沒有發生Exception
  try {
   
   int result = x + y;
   System.out.println("1+0結果=" + result);
  }
  catch (Exception e) {
   System.out.println("不可為0");
  }
  finally {
   //這區塊為最後的處理 ,可有可無。
   System.out.println("測試二結束");
  }
 }
}


另外不建議把例外處理故意當做流程控制用。


返回[JAVA]流程控制語法目錄

2013年1月20日 星期日

[JAVA]使用label

goto--java沒有goto,但使用label與break和continue,效果有點像。



int i = 1;
int j;
System.out.println("break example");
out:while (i <= 4) {
 i++;
 j = 0;
 while (j <= 5) {
  j++;
  if (j == 3) {
   break out;
  }
  System.out.println(j);
 }
}

i = 1;
System.out.println("continue example");
out2:while (i <= 4) {
 i++;
 j = 0;
 while (j <= 5) {
  j++;
  if (j == 3) {
   continue out2;
  }
  System.out.println(j);
 }
}



返回[JAVA]流程控制語法目錄


2013年1月19日 星期六

[JAVA]使用break和continue



使用break和continue,可以跳脫迴圈

break 結束此迴圈
continue跳脫此迴圈一次





int i = 0;
while (i < 10) {
 if (i == 3) {
  System.out.println("continue");
  i++;
  continue;
 }
 System.out.println(i);
 i++;
}
i = 0;
while (i < 10) {
 if (i == 3) {
  System.out.println("break");
  break;
 }
 System.out.println(i);
 i++;
}








返回[JAVA]流程控制語法目錄




[JAVA]使用foreach



for(元素類型: collection){
敘述;
}




 Integer[] numbers = {10, 100, 1000, 10000, 100000, 1000000};
        
        for (Integer mums : numbers) {
            System.out.println("Number: " + mums);
        }
         
        List names = new ArrayList();
        names.add("Jquery");
        names.add("Java");
        names.add("Linux");
         
        for (String name : names) {
            System.out.println("Name: " + name);
        }





















其它文章

[JAVA]使用switch


switch(運算式){
        case 常數1:
                敘述;
                break;
        case常數2:
                敘述;
                break;
        default:
                敘述;
                default 可有可無;
                break;
}
**注意事項:
switch 敘述有一個很大的使用限制。
switch 敘述括號裡的運算式,只能是bytecharshortint這四種型態之一。
其它型態不可用。




int sum = 10;
switch (sum) {
 case 10:
  System.out.println("當sum = 10 則進入此敘述");
  break;
 case 20:
  System.out.println("當sum = 20 則進入此敘述");
  break;
 case 30:
  System.out.println("當sum = 30 則進入此敘述");
  break;
 default:
  System.out.println("當sum的值不符合上述任合一個條件則進入此敘述");
  break;

}


switch (sum) {
 case 10:
  System.out.println("當sum = 10 則進入此敘述,如果沒有break則會往下走");
 case 20:
  System.out.println("當sum = 20 則進入此敘述");
  break;
 case 30:
  System.out.println("當sum = 30 則進入此敘述");
  break;
 default:
  System.out.println("當sum的值不符合上述任合一個條件則進入此敘述");
  break;
}



**也可以用enum

/**
 * 宣告 "Day" enum 型態
 */
enum Day
{
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 
}

/**
 * 顯示中文星期幾
 */

public static void printTodays(Day theDay)
{
 switch (theDay)
 {
  case MONDAY: 
   System.out.println("星期一");
      break;
  case TUESDAY: 
   System.out.println("星期二");
      break;
  case WEDNESDAY: 
   System.out.println("星期三");
      break;
  case THURSDAY:  
   System.out.println("星期四");
      break;

  case FRIDAY:    
   System.out.println("星期五");
      break;

  case SATURDAY:
   System.out.println("星期六");
      break;
  case SUNDAY:    
   System.out.println("星期日");
      break;
  default:        
   System.out.println("@@ 怎麼會顯示我呢?");;
 }
}






















其它文章

[JAVA]使用do while迴圈


do{
 //重覆執行的敘述
}while(判斷式)

int i = 0;
do {
 // 執行區塊
 System.out.println(i);
 i++;
 //判斷式為true者重覆執行區塊
}while (i < 10);





返回[JAVA]流程控制語法目錄















其它文章

[JAVA]使用for迴圈



for(初始值;判斷式;累計值){
//重覆執行的敘述
}


初始值:只會執行一次
判斷式:判斷是否要重覆執行
累計值:計數器,就是每次要增減值

for (int i = 0; i < 10; i++) {
 // 執行區塊
 //判斷式為true者進來執行區塊
 System.out.println(i);
}


















其它文章

[JAVA]使用while迴圈


使用while迴圈會重覆執行區塊的內容

while(判斷式){
        //重覆執行的敘述

int i = 0;
while (i < 10) {
 // 執行區塊
 // while(判斷式為true)者進來執行區塊
 // 判斷式不成立false 者不進來執行區塊
 System.out.println(i);
 i++;
}




返回[JAVA]流程控制語法目錄















其它文章

2012年8月17日 星期五

[JAVA]流程控制 三元運算子 :?



三元判斷式    判斷式?成立程式區塊:不成立程式區塊

三元判斷式 ,
可以說是 if else 的簡寫版,
如果你的if else判斷是簡單的判斷,
可使用這三元判斷式,
多個if else if 判斷,
不建議使用三元判斷式,
因為會使程式複雜化。

// if()
  score = 70;
  if (score == 70) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score:" + score);
  }
  else {// 條件不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }
  
  //判斷式?成立程式區塊:不成立程式區塊 
  score = 70;
  System.out.println(score == 70 ? "條件成立  score:" + score : "不條件成立  score:" + score);
  


返回[JAVA]流程控制語法目錄

2012年8月8日 星期三

[JAVA]流程控制 if-else


說明:
一、

if(true){
//true 條件成立 進入此程式區塊
}

二、
if(true){
//true 條件成立 進入此程式區塊
}else{
//false 條件不成立  進入此程式區塊
}
三、
if( 條件判斷)
{
  條件判斷為true
}else{
 條件判斷為false
}

例:
// if() {} Test 1 -- 條件成立
  int score = 80;
  if (score >= 70) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 70:" + score);
  }
  // if() {} Test 2 -- 條件不成立
  score = 50;
  if (score >= 70) {// 不成立 條件 不進入此程式區塊
   System.out.println("條件成立 score >= 70:" + score);
  }
  // 不成立 條件 會跳過上述程式區塊

  // if() {} else {} Test 1 -- 條件成立
  score = 60;
  if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else {// 條件不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }
  // if() {} else {} Test 2 -- 條件不成立
  score = 59;
  if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else {// 條件不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }

  // if() {} else if() {} else {} Test 
  score = 59;
  if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else if (score == 59) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score == 59:" + score);
  }
  else { // 條件都不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }
  // if() {} else if() {} else {}  可以很多個else if()  Test 
  score = 70;
  if (score >= 90) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 90:" + score);
  }
  else if (score >= 80) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 80:" + score);
  }
  else if (score >= 70) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 70:" + score);
  }
  else if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else if (score >= 50) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 50:" + score);
  }
  else { // 所有條件都不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }


返回[JAVA]流程控制語法目錄

標籤

Oracle (150) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (84) css-基本類 (65) MySQL (59) CSS Selector (58) jQuery (49) JavaScript-基本類 (39) Spring Boot (38) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) linux cent os (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) Spring MVC (11) MySQL-基本系列教學 (10) Notepad (10) Notepad++ (10) SQLite for java (10) Windows (10) c/c++ (10) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) HTML5 (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) Docker (6) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Servlet (6) centos (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) 開發工具 (5) CSS Properties (4) Dev-C++ (4) IntelliJ IDEA (4) Oracle DDL (4) Sublime (4) TortoiseSVN (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) JVM (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Spring web (3) Squid (3) VirtualBox (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) Filter (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) JSP (2) Jboss (2) Listener (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot Actuator (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) WebJars (2) WinMerge (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) putty (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Base64 (1) Google API (1) HTML5-基本類 (1) Heap (1) JAVA 7 (1) JAVA SE 、JAVA EE、JAVA ME (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JDK (1) JMX (1) JRE (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Stack (1) Struts 2 (1) Tool (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) java8 (1) jsoup (1) mockmvc (1) modules (1) tomcat (1) win10 (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 讀書分享 (1) 開發資訊 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...