JAVA语言考试 --编程
发布网友
发布时间:2022-04-29 00:39
我来回答
共3个回答
热心网友
时间:2022-06-26 15:17
第一题
public class Array {
public static void main(String[] args) {
int []a={10,9,8,7,6,5,4,3,2,1};
int []b=new int[10];
int i=0,j=0;
for(j=0;j<10;i++,j++){
if(j<=4)
b[j]=a[i+5];
else
b[j]=a[i-5];
}
for(i=0;i<10;i++){
System.out.print(a[i]+" ");
}
System.out.println();
for(i=0;i<10;i++){
System.out.print(b[i]+" ");
}
}
}
第二题
public class Array2 {
public static void main(String[] args) {
int []a={10,9,8,7,6,5,4,3,2,1};
int []b=new int[10];
int i=0,j=0;
for(j=0;j<10;i--,j++){
b[j]=a[i+9];
}
for(i=0;i<10;i++){
System.out.print(a[i]+" ");
}
System.out.println();
for(i=0;i<10;i++){
System.out.print(b[i]+" ");
}
}
}
热心网友
时间:2022-06-26 15:17
int [] fArray=new int[10];
//...init your fArray;
int temp;
int i=0;
int j=10;
for(i=0;i<5;i++){
temp=fArray[i];
fArray[i]=fArray[i+5];
fArray[i+5]=temp;
}
for(i=0,j=i+5;i<10;i++){
System.out.println("the first items is "+fArray[i]+", and the end items is "+fArray[j+5]);
if(i>5){
j=i-5;
}
}
----------------------------------------------------
int [] fArray=new int[10];
for(int i=0,j=10;i<10;i++,j--){
System.out.println("items is "+fArray[i]+" and changed items is "+fArray[j]");
}
热心网友
时间:2022-06-26 15:18
第一题
public class Test {
static char[] cr={'a','b','c','d','e','f','g','h','i','j'};
public static void sort(char[] cr) {
for(int i=0;i<5;i++){
char temp=cr[i];
cr[i]=cr[i+5];
cr[i+5]=temp;
}
}
public static void main(String[] args) {
for(char c:cr){
System.out.print(c+",");
}
sort(cr);
System.out.println();
for(char c:cr){
System.out.print(c+",");
}
}
}
第二题
public class Test {
public static void main(String[] args) {
char[] cr={'a','b','c','d','e','f','g','h','i','j'};
for(char c:cr){
System.out.print(c+",");
}
System.out.println();
for(int i=0;i<cr.length;i++){
char temp=cr[cr.length-1-i];
cr[cr.length-1-i]=cr[i];
cr[i]=temp;
System.out.print(cr[i]+",");
}
}
}