JAVA上机课,课后四个小题

分别是55页的9,10,12,17小题
<pre name="code" class="java">package njtcstudent.com;

import java.util.Scanner;

public class test {

	public static void main(String[] args) {
		nine();//输入法10个数,输出最大值,最小值
		ten();//鸡兔同笼问题
		twelve();// 输入一个整数,输出各位数字之和
		Seventeen();//求素数因子
	}

	public static void Seventeen() {
		Scanner sc = new Scanner(System.in);
		int num;
		System.out.println("请输入一个整数:");
		num = sc.nextInt();
		for(int i=2;i*i<=num;){
			if(num%i==0){
				System.out.println("---->"+i);
			num/=i;
			}
			else 
				i++;
		}
		System.out.println("---->"+num);

	}

	public static void twelve() {
		Scanner sc = new Scanner(System.in);
		Integer num;
		int sum = 0;
		System.out.println("请输入一个整数:");
		num = sc.nextInt();
		String str = num.toString();
		for (int i = 0; i < str.length(); i++) {
			sum = sum + str.charAt(i) - '0';
		}
		System.out.println("各位数之各为:" + sum);
	}

	public static void ten() {
		int chicken = 1, rabbit = 1;
		for (; chicken < 40; chicken++) {
			rabbit = 40 - chicken;
			if ((chicken * 2 + rabbit * 4 == 100) && chicken + rabbit == 40) {
				break;
			}
		}
		System.out.println("chicken:" + chicken);
		System.out.println("rabbit:" + rabbit);
	}

	public static void nine() {
		Scanner sc = new Scanner(System.in);
		int max = 0, min = 0, temp;
		for (int i = 1; i <= 10; i++) {
			System.out.println("请输入第" + i + "个数:");
			temp = sc.nextInt();
			if (i == 1) {
				max = temp;
				min = temp;
			} else {
				if (temp < min) {
					min = temp;
				}
				if (temp > max) {
					max = temp;
				}
			}
		}
		System.out.println("最大的数是:" + max);
		System.out.println("最小的数是:" + min);
	}

}


 

你可能感兴趣的:(JAVA上机课,课后四个小题)