第一题
package zuoye;
import java.util.Scanner;
public class l1 {
public static void main(String[] args) {
System.out.println("请输入5句话:");
String a[]= new String[5];
Scanner input = new Scanner(System.in);
for (int i = 0; i< a.length; i++) {
a[i]=input.next();
System.out.println("第"+(i+1)+"句话是"+a[i]);
}
System.out.println("倒叙后:");
for (int j = a.length-1; j >= 0 ; j--) {
System.out.println("" +a[j]);
}
}
}
运行结果:
第二题
package zuoye;
import java.util.Arrays;
public class l2 {
public static void main(String[] args) {
int[]array=new int[]{1,3,-1,5,-2};
System.out.println("原数组:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] +",");
Arrays.sort(new int[]{array[i]});
} System.out.println();
int temp = 0;
for (int j = 0; j < array.length-1; j++) {
for (int k = 0; k < array.length-1; k++) {
if (array[k] > array[k+1]){
temp = array[k];
array[k] = array[k+1];
array[k+1] = temp;
}
}
}
System.out.println("排序后:");
for (int o = 0; o< array.length; o++){
System.out.print(array[o]+" ");
}
}
}
运行结果:
原数组:
1,3,-1,5,-2,
排序后:
-2 -1 1 3 5
第三题:
package zuoye;
import java.util.Scanner;
public class l3 {
public static void main(String[] args) {
System.out.println("请输入10个数字:");
int b = 0;
int c = 0;
int d = 0;
Scanner input = new Scanner(System.in);
int a[] = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt(); //数字存入数组
if (a[i] == 1){
b++;
}
if (a[i] == 2){
c++;
}
if (a[i] == 3){
d++;
}
}
System.out.print("输入的10个数为:");
for (int o = 0; o < a.length; o++) {
System.out.print(a[o]+"\t");
}
System.out.print("\n数字1的个数是:"+b);
System.out.print("\n数字2的个数是:"+c);
System.out.print("\n数字3的个数是:"+d);
System.out.print("\n数字非法的个数是:"+(10-b-c-d));
}
}
运行结果:
请输入10个数字:
1
2
3
1
2
3
4
4
4
4
输入的10个数为:1 2 3 1 2 3 4 4 4 4
数字1的个数是:2
数字2的个数是:2
数字3的个数是:2
数字非法的个数是:4
进程已结束,退出代码为 0