吴裕雄--天生自然 JAVA开发学习:方法
/** 返回两个整型变量数据的较大值 */
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2; return result;
}
public class TestMax {
/** 主方法 */
public static void main(String[] args) {
int i = 5;
int j = 2;
int k = max(i, j);
System.out.println( i + " 和 " + j + " 比较,最大值是:" + k);
}
/** 返回两个整数变量较大的值 */
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
public class TestVoidMethod {
public static void main(String[] args) {
printGrade(78.5);
}
public static void printGrade(double score) {
if (score >= 90.0) {
System.out.println('A');
}
else if (score >= 80.0) {
System.out.println('B');
}
else if (score >= 70.0) {
System.out.println('C');
}
else if (score >= 60.0) {
System.out.println('D');
}
else {
System.out.println('F');
}
}
}
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++) {
System.out.println(message);
}
}
public class TestPassByValue {
public static void main(String[] args) {
int num1 = 1;
int num2 = 2;
System.out.println("交换前 num1 的值为:" +
num1 + " ,num2 的值为:" + num2);
// 调用swap方法
swap(num1, num2);
System.out.println("交换后 num1 的值为:" +
num1 + " ,num2 的值为:" + num2);
}
/** 交换两个变量的方法 */
public static void swap(int n1, int n2) {
System.out.println("\t进入 swap 方法");
System.out.println("\t\t交换前 n1 的值为:" + n1
+ ",n2 的值:" + n2);
// 交换 n1 与 n2的值
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println("\t\t交换后 n1 的值为 " + n1
+ ",n2 的值:" + n2);
}
}

// 一个简单的构造函数
class MyClass {
int x; // 以下是构造函数
MyClass() {
x = 10;
}
}
// 一个简单的构造函数
class MyClass {
int x; // 以下是构造函数
MyClass(int i ) {
x = i;
}
}
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
}
}
public class VarargsDemo {
public static void main(String args[]) {
// 调用可变参数的方法
printMax(34, 3, 3, 2, 56.5);
printMax(new double[]{1, 2, 3});
}
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);
}
}
public class FinalizationDemo {
public static void main(String[] args) {
Cake c1 = new Cake(1);
Cake c2 = new Cake(2);
Cake c3 = new Cake(3);
c2 = c3 = null;
System.gc(); //调用Java垃圾收集器
}
}
class Cake extends Object {
private int id;
public Cake(int id) {
this.id = id;
System.out.println("Cake Object " + id + "is created");
}
protected void finalize() throws java.lang.Throwable {
super.finalize();
System.out.println("Cake Object " + id + "is disposed");
}
}

吴裕雄--天生自然 JAVA开发学习:方法的更多相关文章
- 吴裕雄--天生自然 JAVA开发学习:基础语法
package test; public class temp { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(Stri ...
- 吴裕雄--天生自然 JAVA开发学习:变量类型
public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...
- 吴裕雄--天生自然 JAVA开发学习:异常处理
try { // 程序代码 }catch(ExceptionName e1) { //Catch 块 } import java.io.*; public class ExcepTest{ publi ...
- 吴裕雄--天生自然 JAVA开发学习:集合框架
import java.util.*; public class Test{ public static void main(String[] args) { List<String> l ...
- 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库
1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...
- 吴裕雄--天生自然 JAVA开发学习:解决java.sql.SQLException: The server time zone value报错
这个异常是时区的错误,因此只你需要设置为你当前系统时区即可,解决方案如下: import java.sql.Connection ; import java.sql.DriverManager ; i ...
- 吴裕雄--天生自然 JAVA开发学习:Scanner 类
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanne ...
- 吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //使用 BufferedReader 在控制台读取 ...
- 吴裕雄--天生自然 JAVA开发学习:正则表达式
import java.util.regex.*; class RegexExample1{ public static void main(String args[]){ String conten ...
随机推荐
- Spring课程 Spring入门篇 7-3 advice扩展
课程链接: 1 解析 1.1 advice中aspect 切面传参 1.2 通知参数名称——argNames属性, 参数为 JoinPoint.ProceedingJoinPoint.JoinPoin ...
- JNI调用so动态库
1.编写native接口 package org.demo; public class JniDemo { public static native int bmp2fea(byte[] bmp, b ...
- 长篇Essay写作凑字数的小技巧
当一个留学党面对一篇5000字的essay,写一半之后却没法继续~这类的感觉是很多同学无法想象的!此时唯一的一个有效的方法:凑字数!但是essay写作怎么凑字数呢?如何写够5000字essay?下面我 ...
- winfrom窗体的透明度
在VS中创建一个Winform项目,其默认的窗体名称为 Form1. 在VS设计界面中对 Form1 的 Opacity 属性值设置为 50%. 没错,就这样就可以了. 方法2: ...
- C# 使用复选框实现循环添加多条信息入库!
先在前台获取到所有要添加信息的Id $('.imgid').each(function () { if ($(this).attr("src" ...
- C# Stream篇(四) -- FileStream
FileStream 目录: 如何去理解FileStream? FileStream的重要性 FileStream常用构造函数(重要) 非托管参数SafeFileHandle简单介绍 FileStre ...
- centos破解压缩文件密码
rarcrack是linux系统端的一款破解加密压缩包的工具,rarcrack使用的穷举法进行破解已经加密的rar.zip和7z压缩包,支持设置多线程,和文件类型进行破解. 1.rarcrack下载页 ...
- AVCodecContext 结构体
typedef struct AVCodecContext { int bit_rate; int frame_number; //扩展数据,如mov 格式中audio trak 中aac 格式中es ...
- java课程之团队开发第一阶段评论
1.没有UI设计,整体的样式感觉不堪入目 2.功能方面实现的并不是很多,还需要继续努力 3.还需要添加一些常用的课表功能,比如说导入课表等
- jQuery下拉框联动(JQ遍历&JQ中DOM操作)
1.下载jQuery,并导入:https://blog.csdn.net/weixin_44718300/article/details/88746796 2.代码实现: <!DOCTYPE h ...