System.out.println(); //类.对象.方法
修饰符 返回类型值 方法名(参数类型 参数名){ ----- 方法体 ----- return 返回值; }
public static double max(double num1, double num2){} public static int max(int num1, int num2){} public static int max(int num1, int num2, int num3){}
public class CommandLine{ public static void main(String args[]){ for(int i=0;i<args.length;i++){ System.out.println("args["+i+"]"args[i]); } } }
/* 设计一个计算器 - 写四个方法:加减乘除 - 利用循环+switch进行用户交互 - 传递需要操作的两个数 - 输出结果 */ import java.util.Scanner; public class Calculator { public static void main(String[] args) { System.out.println("============="); System.out.println(" Java 计算器"); System.out.println("============="); System.out.println("请输入四则运算式(例 1 + 2):"); Scanner scanner = new Scanner(System.in); double num1; double num2; char operator; double result; while (scanner.hasNextDouble()){ num1 = scanner.nextDouble(); operator = scanner.next().charAt(0); num2 = scanner.nextDouble(); switch (operator){ case '+': result = add(num1, num2); System.out.println(""+num1+operator+num2+"="+result); break; case '-': result = sub(num1, num2); System.out.println(""+num1+operator+num2+"="+result); break; case '*': result = multi(num1, num2); System.out.println(""+num1+operator+num2+"="+result); break; case '/': result = div(num1, num2); System.out.println(""+num1+operator+num2+"="+result); break; default: System.out.println("输入无效"); break; } System.out.println("输入算式继续运算,输入字符则程序结束!"); } } public static double add(double num1, double num2){ return num1+num2; } public static double sub(double num1, double num2){ return num1-num2; } public static double multi(double num1, double num2){ return num1*num2; } public static double div(double num1, double num2){ return num1/num2; } }
运行结果