zoukankan      html  css  js  c++  java
  • JAVA经典小例子(二)

    使用条件结构从键盘输入一个月份,打印输出对应的季节(3~5月为春天)

    import java.util.Scanner;
    public class D6{

    public static void main (String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.println("输入一个月份(1~12)");

    int x=scan.nextInt();

    if(x>=3&&x<=5 ){
    System.out.println("春");

    }else if(x<=8&&x>=6){
    System.out.println("夏");

    }else if(x>=9&&x<=11){
    System.out.println("秋");

    }else {
    System.out.println("冬");

    }
    }

    }

    ---------------------------------------------------------------------------------

    从控制台输入三个整数,按照从小到大的顺序输出


    import java.util.Scanner;
    public class ForCircle {

    public static void main (String[] args){

    Scanner scan = new Scanner(System.in);
    int[] str = new int[3];

    for(int i = 0 ; i < 3 ; i ++){
    System.out.println("请输入第"+(i+1)+"个整数");
    str[i] = scan.nextInt();

    }

    for(int x=0 ; x < str.length-1 ; x++){

    for(int y=0;y < str.length-x-1 ; y++ ){

    int temp;
    if (str[y]>=str[y+1]){
    temp=str[y+1];
    str[y+1]=str[y];
    str[y]=temp;

    }


    }

    }

    System.out.print("num3:"+str[2]+" "+"num2"+str[1]+" "+"num1:"+str[0]);

    }

    }

    --------------------------------------------------------------------------------- 

    从键盘输入年和月,判断该年是否为闰年,并输出对应月份的天数

    import java.util.Scanner;
    public class D{

    public static void main (String[] args){
    Scanner scan = new Scanner(System.in);
    int i=0;
    System.out.println("请输入年份:");
    int year = scan.nextInt();
    System.out.println("请输入月份:");
    int month = scan.nextInt();

    if((year%400==0) || (year%4==0 && year%100!=0)) {

    System.out.println(year+"是闰年");


    }else{

    System.out.println(year+"是平年");
    i=1;
    }

    switch(month ){

    case 1: System.out.println(month +"月有31天");
    break;
    case 2: if(i==0) {
    System.out.println(month +"月有29天");
    break;

    }else{
    System.out.println(month +"月有28天");
    break;


    }
    case 3: System.out.println(month +"月有31天");
    break;

    case 4: System.out.println(month +"月有30天");
    break;

    case 5: System.out.println(month +"月有31天");
    break;

    case 6: System.out.println(month +"月有30天");
    break;

    case 7: System.out.println(month +"月有31天");
    break;

    case 8: System.out.println(month +"月有31天");
    break;

    case 9: System.out.println(month +"月有30天");
    break;

    case 10: System.out.println(month +"月有31天");
    break;

    case 11: System.out.println(month +"月有30天");
    break;

    case 12: System.out.println(month +"月有31天");
    break;
    default: System.out.println("输入错误");


    }


    }

    }

    ---------------------------------------------------------------------------------

  • 相关阅读:
    12.14 Daily Scrum
    12.13 Daily Scrum
    12.12 Daily Scrum
    12.11 Daily Scrum
    12.10 Daily Scrum
    12.9 Daily Scrum
    12.8 Daily Scrum
    M1事后分析汇报以及总结
    alpa开发阶段团队贡献分
    第9周团队作业
  • 原文地址:https://www.cnblogs.com/gengshidong/p/6496338.html
Copyright © 2011-2022 走看看