Java经典面试编程设计题(含答案)
1、程序实现目标:输入一个字符串,将其各个字符对应的ASCII值加5后,输出结果。程序要求:该字符串只包含小写字母,若其值加5后的字符值大于'z',将其转换成从a开始的字符。
1. package com.iotex;
2. public class StringParseASCII {
3.
4. public static void main(String[] args) {
5. System.out.print(stringParseASCII("abx"));
6. }
7.
8. public static String stringParseASCII(String str){
9. StringBuffer result = new StringBuffer();
10. char tmp;
11. for(int i = 0;i 12. tmp = (char)(str.charAt(i)+5); 13. if(tmp > 'z') { 14. result.append('a'); 15. }else { 16. result.append(tmp); 17. } 18. } 19. 20. return result.toString(); 21. } 22. 23. } 1. package com.iotex; 2. 3. public class StringParseASCII { 4. 5. public static void main(String[] args) { 6. System.out.print(stringParseASCII("abx")); 7. } 8. 9. public static String stringParseASCII(String str){ 10. StringBuffer result = new StringBuffer(); 11. char tmp; 12. for(int i = 0;i 13. tmp = (char)(str.charAt(i)+5); 14. if(tmp > 'z') { 15. result.append('a'); 16. }else { 17. result.append(tmp); 18. } 19. } 20. 21. return result.toString(); 22. } 23. 24. } 2、程序实现目标:求一个整型数组中元素的平均值,并统计其中大于和小于此平均值的元素的个数。 程序要求:输入:整型数组中的元素个数及各个元素。 输出:整型数组中元素的平均值,大于和小于此平均值的元素的个数。 1. package com.iotex; 2. 3. import java.util.Arrays; 4. 5. /** 6. * 7. * @author iotex 8. * 2017-5-7下午11:06:29 9. *2.程序实现目标:求一个整型数组中元素的平均值,并统计其中大于和小于此平均值的元素的个数。 10. *程序要求: 11. * 输入:整型数组中的元素个数及各个元素。 12. * 输出:整型数组中元素的平均值,大于和小于此平均值的元素的个数。 13. */ 14. public class CountAvg { 15. public static void main(String[] args) { 16. int[] array = {1,23,4,13,6}; 17. System.out.println(Arrays.toString(array)+"的平均值:"+avg(array)+"\n" + 18. "大于和小于平均值元素的个数分别为:"+Arrays.toString(countAvg(array))); 19. 20. } 21. public static int[] countAvg(int[] array) { 22. int gt = 0; //grater than 23. int lt = 0; //less than 24. int[] result = {0,0}; 25. int average = avg(array); 26. for(int i = 0;i 27. if(array[i]>average) { 28. gt++; 29. }else if(array[i] 30. lt++; 31. } 32. } 33. result[0] = gt; 34. result[1] = lt; 35. return result; 36. 37. } 38. /** 39. * average 40. * @param array 41. * @return 42. */ 43. public static int avg(int[] array) { 44. int average = 0; 45. int sum = 0; 46. for(int i = 0 ;i 47. sum += array[i]; 48. } 49. 50. average = sum/array.length; 51. return average; 52. } 53. 54. } 1. package com.iotex; 2. 3. import java.util.Arrays; 4. 5. public class CountAvg { 6. public static void main(String[] args) { 7. int[] array = {1,23,4,13,6}; 8. System.out.println(Arrays.toString(array)+"的平均值:"+avg(array)+"\n" + 9. "大于和小于平均值元素的个数分别为:"+Arrays.toString(countAvg(array))); 10. 11. } 12. public static int[] countAvg(int[] array) { 13. int gt = 0; //grater than 14. int lt = 0; //less than 15. int[] result = {0,0}; 16. int average = avg(array); 17. for(int i = 0;i 18. if(array[i]>average) { 19. gt++; 20. }else if(array[i] 21. lt++; 22. } 23. } 24. result[0] = gt; 25. result[1] = lt; 26. return result; 27. 28. } 29. /** 30. * average 31. * @param array 32. * @return 33. */ 34. public static int avg(int[] array) { 35. int average = 0; 36. int sum = 0; 37. for(int i = 0 ;i 38. sum += array[i]; 39. } 40. 41. average = sum/array.length; 42. return average; 43. } 44. 45. } 3、手动输入一个存储整数的数组,要求输出数组里面的2个最大值。 实例: 输入:1,2,5,9,84,3,2 输出:84,9 1. package com.iotex; 2. 3. import java.util.Arrays; 4. public class FindMaxTwoNum { 5. public static void main(String[] args) { 6. int[] array = {1,2,5,9,84,3,2}; 7. System.out.println("数组"+Arrays.toString(array)+"里面最大的2个数为:"); 8. findMaxTwoNum(array); 9. //方法二: 10. // 11. 12. public static void findMaxTwoNum(int[] array) { 13. int[] result = {0,0}; 14. for(int i = 0 ;i 15. for(int j = 0;j 16. if(array[j] 17. int tmp; 18. tmp = array[j]; 19. array[j] = array[j+1]; 20. array[j+1] = tmp; 21. } 22. } 23. } 24. 25. System.out.println(array[0]+"、"+array[1]); 26. } 27. 28. } [java] view plain copy print? 1. package com.iotex; 2. 3. import java.util.Arrays; 4. /** 5. * @author iotex 6. * 2017-5-7下午11:35:13 7. *3、手动输入一个存储整数的数组,要求输出数组里面的2个最大值。 8. * 实例: 9. * 输入:1,2,5,9,84,3,2 10. * 输出:84,9 11. */ 12. public class FindMaxTwoNum { 13. public static void main(String[] args) { 14. int[] array = {1,2,5,9,84,3,2}; 15. System.out.println("数组"+Arrays.toString(array)+"里面最大的2个数为:"); 16. findMaxTwoNum(array); 17. //方法二: 18. // 19. } 20. public static void findMaxTwoNum(int[] array) { 21. int[] result = {0,0}; 22. for(int i = 0 ;i 23. for(int j = 0;j 24. if(array[j] 25. int tmp; 26. tmp = array[j]; 27. array[j] = array[j+1]; 28. array[j+1] = tmp; 29. } 30. } 31. } 32. 33. System.out.println(array[0]+"、"+array[1]); 34. } 35. 36. }