1.从键盘输入若干个正数,输出这些数中大于其平均值的数。
import java.io.*;
import java.util.Scanner;
public class exe5_1 {
public static void main(String args[]) {
Scanner mScanner = new Scanner(System.in);
int a[] = new int[10];
for (int i = 0; i < a.length; i++) {
System.out.println("please input the " + (i + 1) + " number: ");
while(!(mScanner.hasNextInt())) {
System.out.println("please input the " + (i + 1) + " number: ");
mScanner.next();
}
a[i] = mScanner.nextInt();
}
biggerThanAve(a);
}
public static void biggerThanAve(int[] a) {
float ave = 0;
float sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
ave = sum / a.length;
for (int i = 0; i < a.length; i++) {
if (a[i] > ave) {
System.out.println("The number " + a[i] + " is bigger than average " + ave);
}
}
}
}
上述代码没有添加正数判断。
2.从键盘输入n个数,求这n个数中的最大数与最小数并输出。
import java.io.*;
import java.util.Scanner;
public class exe5_2 {
public static void main(String args[]) {
Scanner mScanner = new Scanner(System.in);
int a[] = new int[10];
for (int i = 0; i < a.length; i++) {
System.out.println("please input the " + (i + 1) + " number: ");
while(!(mScanner.hasNextInt())) {
System.out.println("please input the " + (i + 1) + " number: ");
mScanner.next();
}
a[i] = mScanner.nextInt();
}
findMaxMin(a);
}
public static void findMaxMin(int[] a) {
int max = a[0];
int min = a[0];
for (int i = 0; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
}
if (min > a[i]) {
min = a[i];
}
}
System.out.println("The max is " + max + ", the min is " + min);
}
}
3.求一个3阶矩阵的对角线上各元素之和。
import java.io.*;
public class exe5_3 {
public static void main(String args[]) {
int a[][] = {{1,2,3},{4,5,6},{7,8,9}};
int sum1 = 0, sum2 = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == j) sum1 += a[i][j];
if ( j == a.length - i - 1) sum2 += a[i][j];
}
}
System.out.println("sum1 = " + sum1 + ", sum2 = " + sum2);
}
}
4.找出4X5矩阵中最小的那个元素,输出其值及所在的行号和列号。
import java.io.*;
public class exe5_4 {
public static void main(String args[]) {
int a[][] = {{100,2,3,13,14},{4,5,6,15,16},{7,8,9,17,18},{10,11,12,19,20}};
int min = a[0][0];
int colum = 0, row = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (min > a[i][j]) {
min = a[i][j];
row = i;
colum = j;
}
}
}
System.out.println("The min is " + min + ", row = " + row + ", colum = " + colum);
}
}
5.产生0~100之间的8个随机整数,并将其升序排序后输出。
import java.io.*;
import java.lang.Math;
import java.util.Arrays;
public class exe5_5 {
public static void main(String args[]) {
int a[] = new int[8];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
}
Arrays.sort(a);
/*for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=0; i < a.length; i++)
System.out.print(a[i]+ " ");
*/
System.out.println(Arrays.toString(a));
}
}
6.15个红球和15个绿球排成一圈,从第1个球开始数,当数到第13个球时,就拿出此球,然后再从下一球开始数,当再数到13个球时又取出此球,如此循环进行,直到仅剩15个球为止,问怎样排法才能使每次选出的球都是红球。
import java.io.*;
public class exe5_6 {
public static void main(String args[]) {
final int N = 30; // all the number of balls.
final int S = 1; // from S to start.
final int M = 13; // M to out.
int b[] = new int[N];
int i, s, w, j;
s = S;
for (i = 1; i <= N; i++) b[i - 1] = i; // Number each ball.
for (i = N; i >= 2; i--) {
s = (s + M - 1) % i; // Calculate the position of the next starting ball.
if (s == 0) s = i; // The position of the last red ball is stored in the variable s.
w = b[s - 1]; // Save the number of the removed ball to the variable w.
for (j = s; j <= i - 1; j++) b[j - 1] = b[j]; // Starting from position s, the contents of the array move forward.
b[j - 1] = w; // Save w to an empty location.
}
System.out.println("\nThe red ball should be save at: ");
for (i = b.length - 1; i >= 15; i--) System.out.println(" " + b[i]);
}
}
7.编写应用程序,比较命令行中给出的两个字符串是否相等,并输出比较的结果。
import java.io.*;
public class exe5_7 {
public static void main(String args[]) {
BufferedReader buf;
String str1 = null, str2 = null;
buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please input the first string:");
try {
str1 = buf.readLine();
} catch(IOException e1) {
}
System.out.print("Please input the second string:");
try {
str2 = buf.readLine();
} catch(IOException e1) {
}
System.out.print("result = " + str1.equals(str2));
}
}
8.从键盘上输入一个字符串和子串开始位置与长度,截取该字符串的子串并输出。
import java.io.*;
public class exe5_8 {
public static void main(String args[]) {
BufferedReader buf;
String str = null, substr = null;
int start = 0, len = 0;
buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please input the first string:");
try {
str = buf.readLine();
} catch(IOException e1) {
}
System.out.print("Please input the start:");
try {
start = Integer.parseInt(buf.readLine());
} catch(IOException e1) {
}
System.out.print("Please input the len:");
try {
len = Integer.parseInt(buf.readLine());
} catch(IOException e1) {
}
substr = str.substring(start - 1, start - 1 + len);
System.out.print("substring is " + substr);
}
}
9.从一个字符串中删除某一给定的字符。
import java.io.*;
public class exe5_9 {
public static void main(String args[]) {
String str = "i am a java engineer.";
char delChar = 'a';
System.out.println(deleteChar(str,delChar));
}
public static String deleteChar(String str, char delChar){
String delStr = "";
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i) != delChar){
delStr += str.charAt(i);
}
}
return delStr;
}
}
注:练习均为博主自己编写,不是标准答案,可能存在问题,可以留言讨论。