zoukankan      html  css  js  c++  java
  • Java学习第五章 之 方法

      1 /**
      2 
      3    方法格式:
      4 
      5                 修饰符   返回值类型   方法名(参数类型 参数名1, 参数类型  参数名2,  参数类型  参数3....){
      6 
      7                       执行语句;
      8 
      9     
     10 
     11                      return 返回值;
     12 
     13                }
     14 
     15 */
     16 
     17 //求矩形的面积
     18 
     19 public class MethodDemo1{
     20 
     21        public static void main(String[] args){
     22 
     23            int area = Area(5,6);
     24 
     25           System.out.println(area);
     26 
     27        }
     28 
     29  
     30 
     31         public static int Area(int weight , int height){
     32 
     33                   return (weight * height); //返回的是int类型
     34 
     35        }
     36 
     37 }
     38 
     39  
     40 
     41 /**
     42 
     43    方法定义:
     44 
     45                  定义无返回值无参数的方法
     46 
     47                  定义无返回值有参数的方法
     48 
     49                  定义有返回值无参数的方法
     50 
     51                 定义有返回值有参数的方法
     52 
     53 */
     54 
     55  
     56 
     57 /*无返回值无参数的方法
     58 
     59   打印一个三角形 
     60 
     61 */
     62 
     63 public class MethodDemo2{
     64 
     65      public static void main (String[] args){
     66 
     67        printRect();
     68 
     69         }
     70 
     71      public static void printRect(){
     72 
     73           for(int i=0 ; i< 9 ; i++){
     74 
     75               for(int j=0 ; j< i ; j++){
     76 
     77                   System.out.println(" * ");
     78 
     79               }
     80 
     81             System.out.println();
     82 
     83           }
     84 
     85      }
     86 
     87 }
     88 
     89  
     90 
     91 /*无返回值有参数的方法*/
     92 
     93 public class MethodDemo3{
     94 
     95     public static void main(String[] args){
     96 
     97          printRect2(5,6);
     98 
     99      }
    100 
    101  
    102 
    103     public static void printRect2(int m , int n){
    104 
    105             for(int i = 0 ; i < m ; i++){
    106 
    107                 for(int j = 0 ; j < n ; j++){
    108 
    109                       System.out.println("*");
    110 
    111                   }
    112 
    113                System.out.println();
    114 
    115              }
    116 
    117      }
    118 
    119 }
    120 
    121  
    122 
    123 /*有返回值无参*/
    124 
    125 public class MethodDemo4{
    126 
    127       public static void main(String[] args){
    128 
    129           int num = getNumber();
    System.out.println(num); 130 131 } 132 133 134 135 public static int getNumber(){ 136 137 Scanner sc = new Scanner(System.in); 138 139140 141 return sc.next; 142 143 } 144 145 } 146 147 148 149 /*有返回值有参*/ 150 151 public class MethodDemo5{ 152 153 public static void main(String[] args){ 154 155 int product = sum(3 , 6); 156 157 System.out.println(product); 158 159 } 160 161 162 163 public static int sum(int a , int b){ 164 165 return a * b; 166 167 } 168 169 }
  • 相关阅读:
    08-JS中table隔行换色
    07-JS中 li 排序
    HTML DOM 事件
    JavaScript 事件
    jQuery事件函数
    JQuery与JS对象相互转换
    jQuery中的选择器
    jQuery实现放大镜特效
    java线程(2016-4-7)
    Java 线程的转换及状态
  • 原文地址:https://www.cnblogs.com/z97-/p/12533472.html
Copyright © 2011-2022 走看看