当前位置: 首页 > 实验教学文件 > 实验任务 > 正文  
最新动态

JAVA(电商11实验一)

来源:实验中心 | 点击: | 录入时间:2012/09/27

1 生成随机数

2 对随机数进行排序

3 对随机数进行统计,将出现最多次数的随机数显示出来,并计算该随机数显示的次数

要求:熟练使用方法

public class RandomTest {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int [] a = getRandomNumber(1,100,100);

System.out.println("max:"+max(a));

System.out.println("min:"+min(a));

getSameNumber(a);

}

public static int[] getRandomNumber(int min, int max ,int length) {

int[] a = new int[length];

int x = 0;

System.out.println("产生随机数");

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

a[x] = (int) (Math.random() * (max - min ) + 1);

System.out.print(a[x]+ " \t");

if((i+1)%5==0) System.out.println();

x++;

}

return a;

}

public static int max(int[] a) {

int max = a[0];

for (int i : a) {

max = max > i ? max : i;

}

return max;

}

public static int min(int[] a) {

int min = a[0];

for (int i : a) {

min = min < i ? min : i;

}

return min;

}

public static void getSameNumber(int[] a) {

try {

int[][] b = new int[a.length][2];

for (int j = 0; j < a.length; j++) {

b[j][0] = a[j];

b[j][1] = 0;

}

for (int i : a) {

for (int j = 0; j < a.length; j++) {

if (i == b[j][0]) {

b[j][1]++;

}

}

}

int x1 = 0, x2 = 0;

for (int j = 0; j < a.length; j++) {

System.out.println(j + " " +b[j][0] + " : " + b[j][1]);

if(x2 {

x1=b[j][0];

x2=b[j][1];

}

}

System.out.println(x1 + " : " + x2);

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println(e.getMessage());

}

}

}

5.26

/**

* @param args

*/

public static void main(String[] args) {

try {

// TODO Auto-generated method stub

final int NUMBER_OF_PRIMES = 100;

final int NUMBER_OF_PER_PRIMES = 10;

int count = 0;

int number = 2;

while (count < number_of_primes) {

//System.out.println(number);

if (IfPrime(number) && IfPalindromic(number)) {

count++;

if (count % NUMBER_OF_PER_PRIMES == 0)

System.out.println(number);

else

System.out.print(String.valueOf(number) + "\t");

}

number++;

}

} catch (RuntimeException e) {

// TODO Auto-generated catch block

System.out.println(e.getMessage());

}

}

public static boolean IfPrime(int number) {

boolean isPrime = true;

for (int i = 2; i < number/2 + 1 ; i++) {

if (number % i == 0)

{

//isPrime = false;

return false;

}

}

return isPrime;

}

public static boolean IfPalindromic(int number) {

String numberStr=String.valueOf(number);

String t1 = numberStr.substring(0, 1);

String t2 = numberStr.substring(numberStr.length()-1,numberStr.length());

//System.out.println("t1:"+t1+" t2:"+t2);

if(t1.equals(t2))

{

return true;

}

else

{

return false;

}

/*

boolean isPalindromic = false;

int m = number;

int s = 0, x;

while (m != 0) {

x = m % 10;

m = m / 10;

s = s * 10 + x;

}

if (s == m)

isPalindromic = true;

return isPalindromic;

*/

}

5.27

public static void main(String[] args) {

final int NUMBER_OF_PRIMES = 100;

final int NUMBER_OF_PER_PRIMES = 10;

int count = 0;

int number = 2;

while (count < number_of_primes) {

if (IfPrime(number) && !IfPalindromic(number) && IfPrime(number)) {

count++;

if (count % NUMBER_OF_PER_PRIMES == 0)

System.out.println(number);

else

System.out.print(number + " ");

}

number++;

}

}

public static boolean IfPrime(int number) {

boolean isPrime = true;

for (int i = 2; i < number 2; i++) {

if (number % i == 0)

isPrime = false;

}

return isPrime;

}

public static boolean IfPalindromic(int number) {

boolean isPalindromic = false;

int m = number;

int s = 0, x;

while (m != 0) {

x = m % 10;

m = m / 10;

s = s * 10 + x;

}

if (s == m)

isPalindromic = true;

return isPalindromic;

}

public static int Palindromic(int number) {

int m = number;

int s = 0, x;

while (m != 0) {

x = m % 10;

m = m / 10;

s = s * 10 + x;

}

if (s == m)

return s;

else return 0;

}

Copyright © 2011 All Rights Reserved