return 8/我想自定义getmax函数的话怎么做
}
public int getMax(){
/.getMax)/,7.outpublic class test{
public static void main(String[] args){
int arrs[] =new int[]{1
}
}
我想java实现javascript 里面 prototype 这样的原型对象,5.,5
System,1},8,4,具体解决方案如下:
解决方案1:
println(getMax(arrs))a) a = arrs[i].lengtharrsi <
}
public static int getMax(int[] arrs){
int a = 0
for (int i = 0,7.outpublic class TEST{
public static void main(String[] args){
int[] arrs =new int[]{1
}
return a,5i++) {
if(arrs[i] >,5
System,1},8,4
解决方案2:
谢谢。。我能不能 .length() 这样的函数自定义创建呢?
解决方案3:
jdk提供一些常用类跟方法方便我们写程序,自己写也一样的
arrs.length Array里面封装好的实例属性
解决方案4:
谢谢!
解决方案5:
7,5
}
public static void main(String[] args){
int arrs[] =new int[]{1:
public static int getMax(int[] arrs){
/.这里是返回最大值
return 8/:
public static void main(String[] args){
int arrs[] =new int[]{1..
System,4,4///,1}.out
System/,5,5,5.,7,8.我想自定义getmax函数的话怎么做
}
改为public int getMax(){
/,1}.out/.getMax),8.这里是返回最大值
return 8
}
改为.println(getMax(arrs)).println(arrs
解决方案6:
方法没有修饰符的例子:
String getName(){ //无修饰符,无参数,有String类型返回值
return "张3"
}
void getName(){ //无修饰符,无参数,无返回值
system.out.println("张3")
}
int add(int a,int b){ //无修饰符,有2个整型参数,有整型返回值
return a+b
}
方法有2种修饰符
1)有public、protected、private三种显示的访问控制修饰符
2)还可以有static 、 final 、abstract 这三个修饰符
方法含有修饰符的例子:
public String getName(){ //public修饰符(公有方法),无参数,有String类型返回值
return "张3"
}
private void getName(){ //无修饰符(私有方法),无参数,无返回值
system.out.println("张3")
}
首先你这个判断逻辑有问题,
问题一:下面这段都是在if(a>b){}之中的,故只有你输入的a>b时,才会执行下面的代码。而如果你输入的a<=b的时候,就会输出max=0。
问题二:下面的代码逻辑也有问题。
if(a>c){max = a
}else{
max = c
}
if(b>c){
max = b
}else{
max = c
}
一般思路是先比较两个数最大的值,这个为max, 然后再比较max与第三个数的值,依此类推
提供一个正确的逻辑给你参考:
int max = aif (b > a) {
max = b
}
if (c > max) {
max = c
}
或者简化成
int max=a>b?a:bmax=max>c?max:c
又或者写成
int max=Math.max(Math.max(a, b), c)