方法重载
- 以下程序先不使用方法重载机制,分析程序的缺点
package javaproject0313;
/*以下程序可以运行,但功能相似
缺点:代码不美观,程序员需要记忆更多的方法名,很辛苦
*/
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(sumInt(10, 20));
System.out.println(sumLong(10L, 20L));
System.out.println(sumDouble(10.0, 20.0));
}
// 定义个计算int类型数据类型的数据求和方法
public static int sumInt(int a, int b) {
return a + b;
}
// 定义个计算long类型数据类型的数据求和方法
public static long sumLong(Long a, Long b) {
return a + b;
}
// 定义个计算double类型数据类型的数据求和方法
public static double sumDouble(Double a, Double b) {
return a + b;
}
}
- 使用方法重载改进
package javaproject0313;
/*使用方法重载机制,解决之前的两个缺点
1、代码整齐美观
2、功能相似的,可以让方法名相同,更易于以后的代码编写
在java语言中,是怎么进行方法区分的呢?
1、java编译器会通过方法名进行区分,但是Java语言允许方法名相同的情况出现
2、如果方法名相同的情况下,编译器会通过方法的参数类型进行方法的区分
*/
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//对于程序员来说,只需要记忆一个方法名即可。
System.out.println(sum(10, 20));
System.out.println(sum(10L, 20L));
System.out.println(sum(10.0, 20.0));
}
// 定义个计算int类型数据类型的数据求和方法
public static int sum(int a, int b) {
return a + b;
}
// 定义个计算long类型数据类型的数据求和方法
public static long sum(Long a, Long b) {
return a + b;
}
// 定义个计算double类型数据类型的数据求和方法
public static double sum(Double a, Double b) {
return a + b;
}
}
- 方法重载的条件
package javaproject0313;
import javax.management.MalformedObjectNameException;
/*什么时候代码会发生方法重载
1、在同一个类中,如果功能1和功能2的功能是相似的,那么可以考虑将他们的方法名一致这样的代码既美观,
又便于后期的代码编写方法重载overload不能随便使用,如果两个功能根本不相关,
那么使用后方法重载的话,会导致无法进行功能的区分
什么时候考虑使用方法重载
1、在同一个类中
2、方法名相同
3、参数列表不同
同时满足以上3个条件,那么我们就可以认定方法和方法之间发生了重载
参数列表不同:参数的类型不同,参数的个数不同,参数的顺序不同
*/
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
m1();
m2(100);
m1(1, 2.0);
m2(1.0, 2);
m3(3);
m3(3.0);
System.out.println(m4());
System.out.println(m5());
}
public static void m1() {
System.out.println("m1无参数执行!");
}
//这个方法的参数个数和上面的方法的参数个数不同
public static void m2(int a) {
System.out.println("m2有一个int参数执行!");
}
public static void m1(int x,double y) {
System.out.println("m1(int"+ x +",double "+y+")");
}
//参数的顺序不同,也算不同
public static void m2(double y,int x) {
System.out.println("m2(double "+y+",int "+x+")");
}
public static void m3(int x) {
System.out.println("m3(int "+x+")");
}
//参数的类型不同
public static void m3(Double x) {
System.out.println("m3(double "+x+")");
}
//方法重复了。方法重载和方法的返回值类型无关,跟方法名和参数列表有关。
public static int m4() {
return 4;
}
/* public static double m4() {
return 1.0;
}*/
//方法重载和修饰符列表无关
public static int m5() {
return 5;
}
/*int m5() {
return 1;
}*/
class Myclass{
//不在同一个类中,不能叫方法重载
public static void m1(int x,int y) {
System.out.println();
}
}
}
- 体验 println()方法重载,SUN公司团队写的,可以直接用,参数类型任意。
- 查看源代码
- C:\Program Files\Java\jdk-17.0.2\lib 解压
- 打开C:\Program Files\Java\jdk-17.0.2\lib\src\java.base\java\io\PrintStream.java
- 查找println的源代码
public void println() {
newLine();
}
/**
* Prints a boolean and then terminate the line. This method behaves as
* though it invokes {@link #print(boolean)} and then
* {@link #println()}.
*
* @param x The {@code boolean} to be printed
*/
public void println(boolean x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a character and then terminate the line. This method behaves as
* though it invokes {@link #print(char)} and then
* {@link #println()}.
*
* @param x The {@code char} to be printed.
*/
public void println(char x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints an integer and then terminate the line. This method behaves as
* though it invokes {@link #print(int)} and then
* {@link #println()}.
*
* @param x The {@code int} to be printed.
*/
public void println(int x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a long and then terminate the line. This method behaves as
* though it invokes {@link #print(long)} and then
* {@link #println()}.
*
* @param x a The {@code long} to be printed.
*/
public void println(long x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a float and then terminate the line. This method behaves as
* though it invokes {@link #print(float)} and then
* {@link #println()}.
*
* @param x The {@code float} to be printed.
*/
public void println(float x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a double and then terminate the line. This method behaves as
* though it invokes {@link #print(double)} and then
* {@link #println()}.
*
* @param x The {@code double} to be printed.
*/
public void println(double x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints an array of characters and then terminate the line. This method
* behaves as though it invokes {@link #print(char[])} and
* then {@link #println()}.
*
* @param x an array of chars to print.
*/
public void println(char[] x) {
if (getClass() == PrintStream.class) {
writeln(x);
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints a String and then terminate the line. This method behaves as
* though it invokes {@link #print(String)} and then
* {@link #println()}.
*
* @param x The {@code String} to be printed.
*/
public void println(String x) {
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
/**
* Prints an Object and then terminate the line. This method calls
* at first String.valueOf(x) to get the printed object's string value,
* then behaves as
* though it invokes {@link #print(String)} and then
* {@link #println()}.
*
* @param x The {@code Object} to be printed.
*/
public void println(Object x) {
String s = String.valueOf(x);
if (getClass() == PrintStream.class) {
// need to apply String.valueOf again since first invocation
// might return null
writeln(String.valueOf(s));
} else {
synchronized (this) {
print(s);
newLine();
}
}
}
逻辑运算符
*优先级: 小括号 > 单目运算符 > 算术运算符 > 比较远算符 > 逻辑运算符 > 条件运算符 > 赋值运算符
*加小括号可以提升程序的易读性
- 短路问题:可以发生在与,或运算符之间,两个操作数的时候
循环的三种结构
- while:先执行条件判断,再执行循环体,
- do while:先执行循环体,再执行条件判断,当条件一次都不成立的时候,do while至少会进行一次
*do while 适用于至少要成立一次循环的场景,否则用while
- for
java循环基础题练习
** 1、有数列为:9,99,999,…,9999999999。要求使用程序计算此数列的和,并在控制台输出结果。(请尝试使用循环的方式生成这个数列并同时在循环中求和)**
public class Main {
public static void main(String[] args) {
//write your code here........
long sum = 0;
long i = 9;
while (i <= 9999999999l) {
sum += i;
i = i * 10 + 9;
}
System.out.println(sum);
}
}
** 2、控制台输入整数,请设计一个死循环,当用户输入非正数时停止输入。请给出用户输入的正整数个数(默认输入个数不超过2147483647)
输入描述:
若干整数,整数之间用空格隔开
输出描述:
控制台输入的正整数个数**
import java.util.*;
public class Main {
public static void main(String[] args) {
int count = 0;
Scanner scanner = new Scanner(System.in);
while(true){
int a = scanner.nextInt();
if(a > 0){
count++;
}else{
break;
}
}
System.out.print(count);
//write your code here......
}
}
3、编写一个方法,该方法的返回值是两个不大于100的正整数的最小公倍数
输入描述:
两个int类型变量作为方法的参数
输出描述:
int类型的变量,值为两个数的最小公倍数
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int m = console.nextInt();
int n = console.nextInt();
int result = getCM(m, n);
System.out.println(result);
}
public static int getCM(int m, int n){
//write your code here......
//计算m、n中较大者
int max = Math.max(m,n);
for(int i = max;i <= m * n;i++){
如果既能被m整除又能被n整除,说明是最小公倍数,直接返回
if(i % m == 0 && i % n == 0){
return i;
}
}
return -1;
}
}
4、一球从h米高度自由落下,每次落地后反弹回原高度的一半再落下,求它在第n次落地时共经过了多少米?第n次反弹多高?
输入描述:
输入小球的初始高度和落地的次数(先输入小球初始高度再输入反弹次数)
输出描述:
输出小球反弹的高度和经过的距离(先输出反弹的高度再输出经过的距离,中间用空格隔开)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float h = scanner.nextFloat();
int n = scanner.nextInt();
float distance = h;
while(n > 0) {
--n;
h = h / 2;
distance += (2 * h);
}
distance -= 2 * h;
System.out.println(String.format("%.3f", h) + " " + String.format("%.3f", distance));
}
}
5、键盘输入任意多个10000以内正整数(负数代表结束),求出它们的平均数,
输入描述:
任意多个正整数
输出描述:
它们的平均数(平均数为double类型,保留两位小数)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double sum = 0;
int count = 0;
double avg =0;
while (scan.hasNext()) {
int a = scan.nextInt();
if(a < 0) {
break;
}else {
sum += a;
count++;
}
}
avg = sum / count;
System.out.println(String.format("%.2f", avg));
}
}
6、请补全预设代码中判断质数的方法。
输入描述:
一个大于1的整数
输出描述:
true/false
import java.util.Scanner;
import java.io.PrintStream;
public class Main {
public static void main(String[] args) {
Main main = new Main();
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println(main.isPrimeNumber(number));
}
public Boolean isPrimeNumber(int number) {
if (number == 2) {
return true;
}
for (int i = 2; i < number ; i++)
if(number % i == 0){
return false;
}
return true;
//write your code here....
}
}
6、输入一个整数,计算它的位数。如果输入的整数不大于0则输出这个数
输入描述:
一个整数
输出描述:
整数的位数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
scan.close();
//write code here......
int count = 0;
while (num > 0) {
count++;
num = num / 10;
}
System.out.print(count);
}
}
break和continue
- break和continue都是用来控制循环结构的,主要作用是停止循环。