java语言程序设计与数据结构7.2

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

emmaing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值