第八次Java作业
package
work;
1
public
class
work {
public
static
void
main(String[] args) {
int
a[]={
10
,
20
,
30
,
40
,
50
};
for
(
int
i =
0
; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
2
package
work;
public
class
work {
public
static
void
main(String[] args) {
char
a[]={
'n'
,
'e'
,
'u'
,
's'
,
'o'
,
'f'
,
't'
,
'e'
,
'd'
,
'u'
,
'c'
,
'a'
,
't'
,
'i'
,
'o'
,
'n'
};
char
[]b=
new
char
[
16
];
System.arraycopy(a,
0
, b,
0
,
16
);
for
(
int
i =
0
; i < a.length; i++) {
System.out.print(a[i]);
}
}
}
3
package
work;
import
java.util.Arrays;
public
class
work {
public
static
void
main(String[] args) {
int
a[]={
1
,
6
,
2
,
3
,
9
,
4
,
5
,
7
,
8
};
Arrays.sort(a);
System.out.println(
"排序后"
);
for
(
int
i =
0
; i < a.length; i++) {
System.out.print(
" "
+a[i]);
}
}
}
4
package
work;
public
class
work {
public
static
void
main(String[] args) {
double
[][]a=
new
double
[
5
][
4
];
for
(
int
i =
0
; i < a.length; i++) {
for
(
int
j =
0
; j < a[i].length; j++) {
a[i][j]=j;
System.out.print(a[i][j]+
" "
);
}System.out.println();
}
}
}
5
package
work;
public
class
work {
public
static
void
main(String[] args) {
int
a[]={
18
,
25
,
7
,
36
,
13
,
2
,
89
,
63
};
int
max=a[
0
];
int
index=
0
;
for
(
int
i =
0
; i < a.length; i++) {
if
(a[i]>max) {
max=a[i];
index=i;
}
}
System.out.println(
"数组中的最大值为"
+max);
System.out.println(
"下标为"
+index);
}
}
作业
6
package
work;
public
class
work {
public
static
void
main(String[] args) {
int
a[]={
18
,
25
,
7
,
36
,
13
,
2
,
89
,
63
};
System.out.println(
"倒序后为:"
);
for
(
int
i =
7
; i >=
0
; i--) {
System.out.print (
" "
+a[i]);
}
}
}
7
package
work;
public
class
work {
public
static
void
main(String[] args) {
int
a[]={
18
,
25
,
7
,
36
,
13
,
2
,
89
,
63
,
25
,
18
};
for
(
int
i =
0
; i < a.length; i++) {
for
(
int
j =
0
; j < a.length; j++) {
if
(a[i]==a[j] && i!=j) {
a[j]=
0
;
}
}
}
for
(
int
i : a) {
System.out.print(
" "
+i);
}
}
8
package
work;
public
class
work {
public
static
void
main(String[] args) {
int
a[]={-
10
,
2
,
3
,
246
,-
100
,
0
,
5
};
int
max=a[
0
];
int
min=a[
0
];
double
sum=
0
;
for
(
int
i =
0
; i < a.length; i++) {
if
(max<a[i]) {
max=a[i];
}
if
(min>a[i]) {
min=a[i];
}
sum+=a[i];
}
sum=sum/
7
;
System.out.println(
"最大值"
+max);
System.out.println(
"最小值"
+min);
System.out.println(
"平均值"
+sum);
}<br>}
9
package
work;
public
class
work {
public
static
void
main(String[] args) {
int
[] a =
new
int
[
20
];
a[
0
] =
1
;
a[
1
] =
1
;
for
(
int
i =
2
; i < a.length; i++) {
a[i] = a[i -
1
] + a[i -
2
];
}
for
(
int
i =
0
; i <
8
; i++) {
System.out.print(a[i] +
" "
);
}
}
}
10
package
work;
import
java.util.Arrays;
import
java.util.Random;
public
class
work {
public
static
void
main(String[] args) {
Random r =
new
Random();
int
a[] =
new
int
[
10
];
for
(
int
i =
0
; i < a.length; i++) {
a[i] = r.nextInt(
101
);
}
for
(
int
i =
0
; i < a.length; i++) {
System.out.print(a[i] +
" "
);
}
Arrays.sort(a);
System.out.println();
for
(
int
i =
0
; i < a.length; i++) {
System.out.print(a[i]+
" "
);
}
}
}