JAVA编程题全集(100题及答案) 下载本文

}

for (i = 0; i < b.length; i++) { System.out.print(b[i] + \ }

} }

【程序31】

题目:将一个数组逆序输出。 import java.util.*; public class lianxi31 {

public static void main(String[] args) { Scanner s = new Scanner(System.in); int a[] = new int[20];

System.out.println(\请输入多个正整数(输入-1表示结束):\ int i=0,j; do{

a[i]=s.nextInt(); i++;

}while (a[i-1]!=-1);

System.out.println(\你输入的数组为:\ for( j=0; j

System.out.print(a[j]+\}

System.out.println(\数组逆序输出为:\ for( j=i-2; j>=0; j=j-1) { System.out.print(a[j]+\} } }

【程序32】

题目:取一个整数a从右端开始的4~7位。 import java.util.*; public class lianxi32 {

public static void main(String[] args) { Scanner s = new Scanner(System.in);

System.out.print(\请输入一个7位以上的正整数:\ long a = s.nextLong();

String ss = Long.toString(a); char[] ch = ss.toCharArray(); int j=ch.length;

if (j<7){System.out.println(\输入错误!\ else {

System.out.println(\截取从右端开始的4~\

7位是:

} } }

【程序33】

题目:打印出杨辉三角形(要求打印出10行如下图) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 ????

public class lianxi33 {

public static void main(String[] args) { int[][] a = new int[10][10]; for(int i=0; i<10; i++) { a[i][i] = 1; a[i][0] = 1; }

for(int i=2; i<10; i++) { for(int j=1; j

a[i][j] = a[i-1][j-1] + a[i-1][j]; } }

for(int i=0; i<10; i++) {

for(int k=0; k<2*(10-i)-1; k++) { System.out.print(\ }

for(int j=0; j<=i; j++) {

System.out.print(a[i][j] + \ }

System.out.println(); } } }

【程序34】

题目:输入3个数a,b,c,按大小顺序输出。 import java.util.Scanner; public class lianxi34 {

public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println(\请输入3个整数:\ int a = s.nextInt(); int b = s.nextInt();

int c = s.nextInt(); if(a < b) { int t = a; a = b; b = t; }

if(a < c) { int t = a; a = c; c = t; }

if(b < c) { int t = b; b = c; c = t; }

System.out.println(\从大到小的顺序输出:\ System.out.println(a + \} }

【程序35】

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 import java.util.*; public class lianxi35 {

public static void main(String[] args) { int N = 8;

int[] a = new int [N];

Scanner s = new Scanner(System.in); int idx1 = 0, idx2 = 0;

System.out.println(\请输入8个整数:\ for(int i=0; i

System.out.println(\你输入的数组为:\ for(int i=0; i

System.out.print(a[i] + \ }

int max =a[0], min = a[0]; for(int i=0; i max) { max = a[i]; idx1 = i; }

if(a[i] < min) {

min = a[i]; idx2 = i; } }

if(idx1 != 0) { int temp = a[0]; a[0] = a[idx1]; a[idx1] = temp; }

if(idx2 != N-1) { int temp = a[N-1]; a[N-1] = a[idx2]; a[idx2] = temp; }

System.out.println(\交换后的数组为:\ for(int i=0; i

System.out.print(a[i] + \ } } }

【程序36】 题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数 import java.util.Scanner; public class lianxi36 {

public static void main(String[] args) { int N =10;

int[] a = new int[N];

Scanner s = new Scanner(System.in);

System.out.println(\请输入10个整数:\ for(int i=0; i

System.out.print(\你输入的数组为:\ for(int i=0; i

System.out.print(a[i] + \ }

System.out.print(\请输入向后移动的位数:\ int m = s.nextInt(); int[] b = new int[m]; for(int i=0; i

for(int i=N-1; i>=m; i--) { a[i] = a[i-m];