最新JAVA编程题全集(50题及答案)(5)

2018-12-22 19:02

}

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 < N; i++) { a[i] = s.nextInt(); }

// 获得输入的八个数字

System.out.println(\你输入的数组为:\); for (int i = 0; i < N; i++) { System.out.print(a[i] + \); }

// 输出输入的数组

int max = a[0], min = a[0]; for (int i = 0; i < N; i++) { if (a[i] > max) { max = a[i]; idx1 = i; }

// 找出最大的数和其下标 if (a[i] < min) { min = a[i]; idx2 = i;

21

}

}

}// 找出最大的数和其下标 }

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 < N; 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--) {

22

a[i] = a[i-m]; }

for(int i=0; i

System.out.print(\位移后的数组是:\ for(int i=0; i

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

【程序37】

题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。 import java.util.Scanner; public class lianxi37 {

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

System.out.print(\请输入排成一圈的人数:\ int n = s.nextInt();

boolean[] arr = new boolean[n]; for(int i=0; i

while(leftCount > 1) { if(arr[index] == true) { countNum ++;

if(countNum == 3) { countNum =0;

arr[index] = false; leftCount --; } }

index ++;

if(index == n) { index = 0; } }

for(int i=0; i

System.out.println(\原排在第\位的人留下了。\

23

} } } }

【程序38】 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 /*………………

*……题目意思似乎不能用length()函数 */ import java.util.*; public class lianxi38 {

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

System.out.println(\请输入一个字符串:\ String str = s.nextLine();

System.out.println(\字符串的长度是:\ } }

【程序39】

题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数) //没有利用指针函数 import java.util.*; public class lianxi39 {

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

System.out.print(\请输入一个正整数 n= \ int n = s.nextInt();

System.out.println(\相应数列的和为:\ }

public static double sum(int n) { double res = 0; if(n % 2 == 0) {

for(int i=2; i<=n; i+=2) { res += (double)1 / i; } } else {

for(int i=1; i<=n; i+=2) { res += (double)1 / i ; } }

return res; } }

【程序40】

24

题目:字符串排序。 public class lianxi40 {

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

String temp = null;

String[] s = new String[N]; s[0] = \ s[1] = \ s[2] = \ s[3] = \ s[4] = \

for(int i=0; i

if(compare(s[i], s[j]) == false) { temp = s[i]; s[i] = s[j]; s[j] = temp; } } }

for(int i=0; i

static boolean compare(String s1, String s2) { boolean result = true;

for(int i=0; i s2.charAt(i)) { result = false; break;

} else if(s1.charAt(i)

if(s1.length() < s2.length()) { result = true; } else {

result = false; } } }

return result; } }

25


最新JAVA编程题全集(50题及答案)(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:编译原理:在线考试系统设计与实现

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: