修饰符 返回值类型 方法名(参数类型 参数名){
...
方法体
...
return 返回值
}
public static void main(String[] args){
}
int larger = max(30,40);
System.out.println("Hello,quan!");
package com.ink.method;
public class Demo01 {
// main方法 修饰符 返回值
public static void main(String[] args) {
// 实际参数:实际调用传递给他的参数
// int sum = add(1, 2);
// System.out.println(sum);
test();
}
// 加法
// 形式参数,用来定义作用的
public static int add(int a,int b){
return a + b;
}
public static void test(){
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){// 每行
System.out.println();
// System.out.println("\n");
}
}
}
}
public calss CommandLine{
public static void main(String[] args){
for(int i = 0;i < args.length;i++){
System.out.println("args["+ i +"]:"+args[i]);
}
}
}
package com.ink.method;
public class Demo02 {
public static void main(String[] args) {
int max = max(10, 20);
System.out.println(max);
}
// 比大小
public static double max(double num1,double num2){
double result = 0;
if (num1==num2){
System.out.println("num1==num2");
return 0;// 终止方法
}
if (num1>num2){
result = num1;
}else {
result = num2;
}
return result;
}
// 比大小
public static int max(int num1,int num2){
int result = 0;
if (num1==num2){
System.out.println("num1==num2");
return 0;// 终止方法
}
if (num1>num2){
result = num1;
}else {
result = num2;
}
return result;
}
}
public static void printMax(double... numbers){
if(numbers.length == 0){
System.out.println("No argument passed");
return;
}
double result = numbers[0];
// 排序!
for (int i = 1;i < numbers.length;i++){
if(numbers[i] > result){
result = numbers[i];
}
}
System.out.println("The max value is "+ result);
}
package com.ink.method;
public class Demo04 {
public static void main(String[] args) {
// Demo04 demo04 = new Demo04();
// demo04.test(1,2,3,4,5,6);
printMax(34,3,5,56.5,22.2);
printMax(new double[]{1,2,3});
}
public void test(int x,int... i){
System.out.println(i[0]);
System.out.println(i[1]);
System.out.println(i[2]);
System.out.println(i[3]);
System.out.println(i[4]);
System.out.println(i[5]);
}
public static void printMax(double... numbers){
if(numbers.length == 0){
System.out.println("No argument passed");
return;
}
double result = numbers[0];
// 排序!
for (int i = 1;i < numbers.length;i++){
if(numbers[i] > result){
result = numbers[i];
}
}
System.out.println("The max value is "+ result);
}
}
A方法调用B方法
递归就是:A方法调用A方法!自己调用自己!
利用递归可以用简单程序来解决一些复杂的问题。它通常把一个大型复杂的问题层层转化为一个原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。递归的的能力在于用有限的语句来定义对象的无限集合
递归的结构包括两个部分:
package com.ink.method;
public class Demo06 {
// 递归思想
public static void main(String[] args) {
System.out.println(f(5 ));// 基数过大 不易使用递归!!!!!!电脑会吃不消!!!!
}
// 1! 1
// 5! 5*4*3*2*1
public static int f(int n){
if (n==1){
return 1;
}else {
return n*f(n-1);
}
}
}
package com.ink.method;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
System.out.println("欢迎使用四则运算计算器!");
System.out.print("请输入第一个数字:");
double num1 = a.nextDouble();
System.out.print("请输入运算符:");
String operator = a.next();
System.out.print("请输入第二个数字:");
double num2 = a.nextDouble();
double result = 0;
switch (operator){
case "+":
result = add(num1,num2);
System.out.println("输入得到的算式为:"+ num1 +operator + num2 + "=" + result );
break;
case "-":
result = reduce(num1,num2);
System.out.println("输入得到的算式为:"+ num1 +operator + num2 + "=" + result );
break;
case "*":
result = multiply(num1,num2);
System.out.println("输入得到的算式为:"+ num1 +operator + num2 + "=" + result );
break;
case "/":
result = divide(num1,num2);
System.out.println("输入得到的算式为:"+ num1 +operator + num2 + "=" + result );
break;
default:
System.out.println("符号输入错误,请重新输入");
}
boolean Formula = true;
while (Formula){
System.out.println("是否继续使用?"+"YES/NO");
String choose = a.next();
if (choose.equals("YES")){
System.out.println("继续使用");
break;
}else if (choose.equals("NO")){
System.out.println("欢迎下次使用!再见!");
return;
}else {
System.out.println("输入不正确,请重新输入");
}
}
}
public static double add(double a ,double b){
return a + b;
}
public static double reduce(double a ,double b){
return a - b;
}
public static double multiply(double a ,double b){
return a * b;
}
public static double divide(double a,double b){
return a / b;
}
}