您的当前位置:首页正文

Java博客记录八--练习

2024-11-10 来源:个人技术集锦
输入数字1~7,输出对应星期几
package jian.yi.li;

import java.util.Scanner;

public class Week {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        switch (a){
            case 1 :
                System.out.println("星期一");
                break;
            case 2 :
                System.out.println("星期二");
                break;
            case 3 :
                System.out.println("星期三");
                break;
            case 4 :
                System.out.println("星期四");
                break;
            case 5 :
                System.out.println("星期五");
                break;
            case 6 :
                System.out.println("星期六");
                break;
            case 7 :
                System.out.println("星期天");
                break;
            default:
                System.out.println("输入有误");
                break;
        }
    }
}

鸡兔同笼的问题

package jian.yi.li;

import java.util.Scanner;

public class SameCage {
    public static void main(String[] args) {
        //鸡兔共35只,共94只脚,那么鸡和兔各几只?
        for(int hen = 0;hen <= 35;hen++){
            if(hen*2 + (35-hen)*4 == 94){
                System.out.println(hen);
                System.out.println((35 - hen));
            }
        }
    }
}
打印全三角形
package jian.yi.li;

import java.util.Scanner;

public class PrintTraingle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        for(int i = 1;i <= a;i++){
            for(int j = 0;j <= a-i;j++){
                System.out.print(" ");
            }
            for(int h = 1;h <= (2*i-1);h++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Top