admin 管理员组文章数量: 1184232
2024年3月11日发(作者:jovi vision img是什么意思)
public class ArrayExecDemo01{
public static void main(String args[]){
int x[] = {1,2,34,4,5,6,7,8,89,8,3,234,24,24,24,24,42,22,43,45,78} ;
int sum1 = 0 ; // 保存奇数个数
int sum2 = 0 ; // 保存偶数个数
for(int i=0;i<;i++){
if(x[i]%2==0){
sum2++ ;
}else{
sum1++ ;
}
}
n("奇数的个数是:" + sum1) ;
n("偶数的个数是:" + sum2) ;
}
}
2
public class ArrayExecDemo02{
public static void main(String args[]){
int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
int newArr[] = new int[nozeroCount(oldArr)] ;
fillArray(oldArr,newArr) ;
print(newArr) ;
}
public static void print(int x[]){
for(int y : x){
(y + "、") ;
}
}
public static void fillArray(int t1[],int t2[]){
int foot = 0 ;
for(int x=0;x<;x++){
if(t1[x]!=0){
t2[foot] = t1[x] ;
foot++ ; // 修改下标
}
}
}
public static int nozeroCount(int temp[]){
int sum = 0 ;
for(int x=0;x<;x++){
if(temp[x]!=0){
sum++ ; // 求出不为0的整数的个数
}
}
3
}
}
return sum ;
public class ArrayExecDemo03{
public static void main(String args[]){
int oldArr[] = {1,3,4,5,11,67,6,6,22,5,4,7,6,7,9,5} ;
int result[] = {0,0,0} ; // 定义一个数组,数组中的第一个元素保存sum、第二个保
存max、第三个保存min
result(oldArr,result) ;
n("数组和是:" + result[0]) ;
n("最大值是:" + result[1]) ;
n("最小值是:" + result[2]) ;
}
public static void result(int x[],int res[]){
res[1] = x[0] ; // 第一个元素为最大值
res[2] = x[0] ; // 第一个元素为最小值
for(int i=0;i<;i++){
res[0] += x[i] ;
if(res[1] res[1] = x[i] ; } if(res[2]>x[i]){ res[2] = x[i] ; } } } } 4 public class ArrayExecDemo04{ public static void main(String args[]){ int oldArr[] = {1,3,4,5,11,67,6,6,22,5,4,7,6,7,9,5} ; n(search(oldArr,33)?"数字已查到!":"数字未查到!") ; } public static boolean search(int x[],int val){ boolean flag = false ; // 默认表示没有此内容 for(int i=0;i<;i++){ if(x[i]==val){ flag = true ; break ; }
版权声明:本文标题:Java开发实战经典—第4章课后答案 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1710137389a559072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论