欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 算法练习1

程序员文章站 2022-03-10 10:36:49
输入一个数,计算所有的位数之和import java.util.Scanner;public class sum{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int sum = 0; for(int i = n; i >0; i/=10){ s....

输入一个数,计算所有的位数之和

import java.util.Scanner;
public class sum{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for(int i = n; i >0; i/=10){
            sum += i % 10;
        }
        System.out.println(sum);
    }
}

计算各图形的面积

import java.util.Scanner;
import java.lang.Math;
public class range{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("1. 长方形");
        System.out.println("2. 正方形");
        System.out.println("3. 三角形");
        System.out.println("4. 圆形");
        int n = sc.nextInt();
        switch (n) {
            case 1:
                System.out.println("请输入长");
                int x = sc.nextInt();
                System.out.println("请输入宽");
                int y = sc.nextInt();
                System.out.println("面积是:"+x*y);
                break;
            case 2:
                System.out.println("请输入边长");
                int l = sc.nextInt();
                System.out.println("面积是:"+l*l);
                break;
            case 3:
                System.out.println("请输入底");
                int b = sc.nextInt();
                System.out.println("请输入高");
                int h = sc.nextInt();
                System.out.println("面积是:"+b*h/2);
                break;
            case 4:
                System.out.println("请输入半径");
                int r = sc.nextInt();
                System.out.println("面积是:" +Math.PI*r*r);
                break;
        
            default:
                break;
        }
    }
}

输入年月,输出天数

import java.util.Scanner;
public class month{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年:");
        int year = sc.nextInt();
        System.out.println("请输入月:");
        int month = sc.nextInt();
        int[] run = {31,29,31,30,31,30,31,31,30,31,30,31};
        int[] pin = {31,28,31,30,31,30,31,31,30,31,30,31};
        if (year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 )) {
            switch (month) {
                case 1:
                    System.out.println("有"+run[0]+"天");
                    break;
                case 2:
                    System.out.println("有"+run[1]+"天");
                    break;
                case 3:
                    System.out.println("有"+run[2]+"天");
                    break;
                case 4:
                    System.out.println("有"+run[3]+"天");
                    break;
                case 5:
                    System.out.println("有"+run[4]+"天");
                    break;
                case 6:
                    System.out.println("有"+run[5]+"天");
                    break;
                case 7:
                    System.out.println("有"+run[6]+"天");
                    break;
                case 8:
                    System.out.println("有"+run[7]+"天");
                    break;
                case 9:
                    System.out.println("有"+run[8]+"天");
                    break;
                case 10:
                    System.out.println("有"+run[9]+"天");
                    break;
                case 11:
                    System.out.println("有"+run[10]+"天");
                    break;
                case 12:
                    System.out.println("有"+run[11]+"天");
                    break;
            
                default:
                    break;
            }
        }  else{
            switch (month) {
                case 1:
                    System.out.println("有"+pin[0]+"天");
                    break;
                case 2:
                    System.out.println("有"+pin[1]+"天");
                    break;
                case 3:
                    System.out.println("有"+pin[2]+"天");
                    break;
                case 4:
                    System.out.println("有"+pin[3]+"天");
                    break;
                case 5:
                    System.out.println("有"+pin[4]+"天");
                    break;
                case 6:
                    System.out.println("有"+pin[5]+"天");
                    break;
                case 7:
                    System.out.println("有"+pin[6]+"天");
                    break;
                case 8:
                    System.out.println("有"+pin[7]+"天");
                    break;
                case 9:
                    System.out.println("有"+pin[8]+"天");
                    break;
                case 10:
                    System.out.println("有"+pin[9]+"天");
                    break;
                case 11:
                    System.out.println("有"+pin[10]+"天");
                    break;
                case 12:
                    System.out.println("有"+pin[11]+"天");
                    break;
            
                default:
                    break;
            }
        } 
    }
}

本文地址:https://blog.csdn.net/weixin_45773503/article/details/107689846