java语言程序设计与数据结构7.2
只需要改动for循环里的一个条件即可,这道题的格式化输出可以记录一下。
package two;
public abstract class T7_6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int NUMBER_OF_PRIMES = 50; // Number of primes to display
final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line
int count = 0; // Count the number of prime numbers
int number = 2; // A number to be tested for primeness
System.out.println("The first 50 prime numbers are \n");
// Repeatedly find prime numbers
while (count < NUMBER_OF_PRIMES) {
// Assume the number is prime
boolean isPrime = true; // Is the current number prime?
// Test if number is prime
for (int divisor = 2; divisor <= Math.sqrt(number) ; divisor++) {
if (number % divisor == 0) { // If true, number is not prime
isPrime = false; // Set isPrime to false
break; // Exit the for loop
}
}
// Print the prime number and increase the count
if (isPrime) {
count++; // Increase the count
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
// Print the number and advance to the new line
System.out.printf("%4d\n",number);
}
else
System.out.printf("%4d",number);
}
// Check if the next number is prime
number++;
}
}
}
/**
程序清单5-15
public class PrimeNumber {
public static void main(String[] args) {
final int NUMBER_OF_PRIMES = 50; // Number of primes to display
final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line
int count = 0; // Count the number of prime numbers
int number = 2; // A number to be tested for primeness
System.out.println("The first 50 prime numbers are \n");
// Repeatedly find prime numbers
while (count < NUMBER_OF_PRIMES) {
// Assume the number is prime
boolean isPrime = true; // Is the current number prime?
// Test if number is prime
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) { // If true, number is not prime
isPrime = false; // Set isPrime to false
break; // Exit the for loop
}
}
// Print the prime number and increase the count
if (isPrime) {
count++; // Increase the count
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
// Print the number and advance to the new line
System.out.println(number);
}
else
System.out.print(number + " ");
}
// Check if the next number is prime
number++;
}
}
}
*/
遍历这100个整数num,counts[num]++
package two;
public class T7_7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=100;
int[] num=new int[n];
int[] counts=new int[10];
for(int e:num) {
e=(int)(Math.random()*10);
counts[e]++;
}
for(int i=0;i<10;i++) {
System.out.println("The number "+i+" appears "+counts[i]+" times.");
}
}
}
package two;
import java.util.Scanner;
public class T7_8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
double[] array=new double[10];
System.out.print("Enter 10 double numbers: ");
for(int i=0;i<array.length;i++)
array[i]=input.nextDouble();
double aver=average(array);
System.out.println(aver);
}
public static int average(int[] array) {
int aver=0;
for(int e:array) {
aver=aver+e;
}
return aver/array.length;
}
public static double average(double[] array) {
double aver=0;
for(double e:array) {
aver=aver+e;
}
return aver/array.length;
}
}
//1 1 1 1 1 1 1 1 1 1
//12.3 23.4 34.5 45.6 56.7 67.8 78.9 89.0 90.1 1.2
package two;
import java.util.Scanner;
public class T7_9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int n=input.nextInt();
double[] array=new double[n];
System.out.print("Enter n double numbers: ");
for(int i=0;i<array.length;i++)
array[i]=input.nextDouble();
double min=min(array);
System.out.println("min of array is "+min);
}
public static double min(double[] array) {
double min=array[0];
for(double e:array) {
if(min>e)
min=e;
}
return min;
}
}
//10
//1 4 5 3 6 7 2 4 0 22
package two;
import java.util.Scanner;
public class T7_10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int n=input.nextInt();
double[] array=new double[n];
System.out.print("Enter n double numbers: ");
for(int i=0;i<array.length;i++)
array[i]=input.nextDouble();
int minIndex=index0fSmallestElement(array);
System.out.println("minIndex is "+minIndex);
}
public static int index0fSmallestElement(double[] array) {
int minIndex=0;
double min=array[0];
for(int i=0;i<array.length;i++) {
if(min>array[i]) {
min=array[i];
minIndex=i;
}
}
return minIndex;
}
}
//2.2 3.1 0.7 3.3 0.7 5.6 8.8 7.6 9.9 9.9