Java日志第49天 2020.8.25
例4.1 在主函数中调用其他函数
public class Demo4_1 {
public static void main(String[] args) {
printstar();
print_message();
printstar();
}
private static void print_message() {
System.out.println("Welcome to java!");
}
private static void printstar() {
//输出30个"*"
System.out.println("******************************");
}
}
例4.2 调用函数时的数据传递
import java.util.Scanner;
public class Demo4_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b, c;
System.out.print("Please enter two integer nembers:");
a = sc.nextInt();
b = sc.nextInt();
c = max(a, b);
System.out.println("max="+c);
}
private static int max(int a, int b) {
int z;
z = a > b ? a : b;
return z;
}
}
例4.3 对被调用的函数做声明
import java.util.Scanner;
public class Demo4_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float a, b, c;
System.out.print("Please enter a, b:");
a = sc.nextFloat();
b = sc.nextFloat();
c = add(a, b);
System.out.println("sum = "+c);
}
private static float add(float a, float b) {
return (a+b);
}
}
例4.4 函数指定为内置函数
public class Demo4_4 {
public static void main(String[] args) {
int i = 10;
int j = 20;
int k = 30;
int m;
m = max(i, j, k);
System.out.println("max = "+m);
}
public static final int max (int a,int b,int c) {
if(b>a) {
a = b;
}
if(c>a) {
a = c;
}
return a;
}
}
例4.5 求3个数中最大的数(分别考虑整数、双精度数、长整数的情况)
import java.util.Scanner;
public class Demo4_5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i1, i2, i3, i;
i1 = sc.nextInt();
i2 = sc.nextInt();
i3 = sc.nextInt();
i = max(i1,i2,i3);
System.out.println("i_max = "+i);
double d1, d2, d3, d;
d1 = sc.nextDouble();
d2 = sc.nextDouble();
d3 = sc.nextDouble();
d = max(d1,d2,d3);
System.out.println("d_max = "+d);
long g1, g2, g3, g;
g1 = sc.nextLong();
g2 = sc.nextLong();
g3 = sc.nextLong();
g = max(g1,g2,g3);
System.out.println("g_max = "+g);
}
public static int max(int a, int b, int c){
if (b>a)
a=b;
if (c>a)
a=c;
return a;
}
public static double max (double a, double b, double c) {
if (b>a)
a=b;
if (c>a)
a=c;
return a;
}
public static long max(long a, long b, long c) {
if (b>a)
a=b;
if (c>a)
a=c;
return a;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南