Java学习实例(3)——打印输出九九乘法表、三元运算符求三个数中的最大值、百鸡百钱问题、计算阶乘

1、打印输出九九乘法表

public class Mul99 {
    public static void main(String[] args) {
        //打印输出九九乘法表
        for(int i = 1 ; i < 10 ; i++){
            for(int j = 1 ; j <= i ; j++){
                int mul = i * j;
                System.out.print( i+"*"+j+"="+mul+" ");
                if(i == j){
//                    System.out.println(" ");
                    System.out.print("\n");
                }
            }

        }
    }
}

 

2、使用三元运算符求三个数中的最大值

//导包
import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {

        //使用三元运算符求三个数中的最大值
        int a = 13;
        int b = 45;
        int c = 36;
        System.out.println(a > b ? (a > c ? a : c) : (b > c ? b : c) );

        //键入三个数,判断最大值
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int z = sc.nextInt();
        if( x > y){
            if( x > z ){
                System.out.println("最大的数为:" + x);
            }else{
                System.out.println("最大的数为:" + z);
            }
        }else{
            if( y > z ){
                System.out.println("最大的数为:" + y);
            }else{
                System.out.println("最大的数为:" + z);
            }
        }
       
    }
}

 

3、打印三角形的*

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //打印出直角三角*
        for(int i = 1 ; i <= 4 ; i++){
            for(int j = 1 ; j <= i ;j++){
                System.out.print("*");
            }
            //换行
            System.out.println();
        }
    }
}

 

4、打印倒三角的*

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //打印倒三角*
        for(int i = 1 ; i <= 4 ; i++){
            for (int j = 1 ; j <= 4-i ; j++){
                System.out.print(" ");
            }
            for (int j = 1 ; j <= i ; j++){
                System.out.print("*");
            }
            //换行
            System.out.println();
        }

    }
}

 

5、百鸡百钱问题

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //百鸡百钱问题
        //公鸡cock、母鸡hen、小鸡chick
        for( int cock = 0; cock <= 100 ; cock++){
            for(int hen = 0; hen <= 100-cock ; hen++){
                int chick = 100 - cock - hen;
                if(cock * 5 + hen * 3 + chick /3 == 100 && chick % 3 == 0){
                    System.out.println("公鸡是:"+cock+"只,"+"母鸡是:"+hen+"只,"+"小鸡是:"+chick+"只。");
                }
            }
        }
    }
}

 

6、定义方法计算阶乘

import java.util.Scanner;

public class Demo02 {

    public static void main(String[] args) {
        System.out.println("请输入要计算阶乘的整数:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        factorial(num);

    //定义方法计算阶乘
    public static void factorial(int n){
        int factor = 1;
        if(n > 0){
            for (int i = 1; i <= n ;i++){
                factor *= i;
            }
            System.out.println("输入的数的阶乘为:"+factor);
        }else if(n == 0){
            System.out.println("0的阶乘为1");
        }else{
            System.out.println("输入非法,请重新输入!");
        }
    }
   
}

 

7、定义方法计算1+2+...+n的和并将结果输出

import java.util.Scanner;

public class Demo02 {

    public static void main(String[] args) {
        System.out.println("请输入n的值:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = add(n);
        System.out.println("累加结果为:"+result);
    }
    //计算1+2+...+n的和并将结果输出
    public static  int add(int n){
        int sum = 0;
        for(int i = 1; i <= n ; i++){
            sum += i;
        }
        return sum;
    }
}

 

8、定义一个方法,接受一个整数n,输出这个数的所有的因子j

import java.util.Scanner;

public class HomeWork {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个整数n: ");
        int n = sc.nextInt();
        factor(n);

    }
    //定义一个方法,接受一个整数n,输出这个数的所有的因子j
    public static void factor(int n){
        System.out.println(n + "因子有:" );
        for(int i = 1; i <= n/2 ;i++){
            for(int j = n ; j >= i ;j--){
                if(i * j == n){
                    System.out.print( i +" "+ j +" ");
                }
            }
        }
    }
}

 

9、定义一个方法,接受一个整数,输出这个整数是几位数

import java.util.Scanner;

public class HomeWork {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个整数n: ");
        int n = sc.nextInt();
        System.out.println("输入整数n的位数为:"+numDigit(n));
    }


    //定义一个方法,接受一个整数,输出这个整数是几位数
    public static int numDigit(int n) {
        int i = 0;
        while (n != 0) {
            n /= 10;
            i++;
        }
        return i;
    }
   
}

 

10、求100 ---- 999之间,满足该数的每位数字的阶乘之和等于该数的数是哪个数.

import java.util.Scanner;

public class HomeWork {
    public static void main(String[] args) {
        factAdd();
    }

    //求100 ---- 999之间,满足该数的每位数字的阶乘之和等于该数的数是哪个数.
    public static void factAdd(){
        for(int i = 100;i < 1000 ; i++){
            int h = i / 100;
            int t = i % 100 / 10;
            int o = i % 10;
            if(fact(h)+fact(t)+fact(o) == i){
                System.out.println("100 ---- 999之间,满足该数的每位数字的阶乘之和等于该数的数是"+i);
            }
        }
    }
    //n的阶乘
    public static int fact(int n){
        int fact = 1;
        for(int i = 1; i <= n ;i++){
            fact *= i;
        }
        return fact;
    }
}

 

posted @ 2021-04-07 16:16  泰初  阅读(496)  评论(0编辑  收藏  举报