【Java】实验一 环境搭建与基本语法

1、编写一个控制台程序,输入一个日期,求下一天的日期。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int month;
        int year;
        int day;
          System.out.println("请输入年份,月份,日期");
        year = s.nextInt();
        month = s.nextInt();
        day = s.nextInt();
        check(year,month,day);
    }

    public static void check(int y , int m , int d) {
        switch(m) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:if(d+1 > 31) {
            m++;
            d=1;
        }
                else
                    d++;
                break;
        case 4:
        case 6:
        case 9:
        case 11:if(d+1 > 30) {
            m++;
            d=1;
        }
        else
            d++;
        break;
        case 12:if(d+1 > 31) {
            y++;
            m=1;
            d=1;
        }
        else 
            d++;
        break;
        case 2:if(y%4==0 && y%100!=0 || y%400==0) {
            if(d+1>29) {
                m++;
                d=1;
            }
            else 
                d++;
        }
        else {
            if(d+1 > 28) {
                m++;
                d=1;
            }
            else
                d++;
        }
        break;
        }
        System.out.println("下一天为"+y+"年"+m+"月"+d+"日");
    }
}


———————————————————————————————————————————

2、编写函数,实现一维及二维数组的复制,并输出复制后的数组

import java.util.Scanner;
public class Main2 {
    static Scanner s=new Scanner(System.in);

    public static void main(String[] args) {
      int t;
      System.out.println("复制一维数组请按1,复制二维数组请按2");
      t = s.nextInt();
      
      if(t==1) {
          int one1;
          System.out.println("请输入数组长度");
          one1 = s.nextInt();
          int cone[] = copyone(one1);
          System.out.print("复制成功:");
          for(int i=0; i<one1 ;i++) {
              System.out.print(cone[i]+" ");
          }
      }
      
      if(t==2) {
          int two1;
          int two2;
          int k = 0;
          System.out.println("请输入一维长度和二维长度");
          two1 = s.nextInt();
          two2 = s.nextInt();
          int ctwo[][] = copytwo(two1,two2);
          System.out.print("复制成功:");
          for(int i=0 ;i<two1 ;i++) {
              for(int j=0 ;j<two2 ;j++) {
                  if(k % two1 == 0)
                      System.out.println();
                  System.out.print(ctwo[i][j]+" ");
                  k++;
              }
          }
      }
    }
    
    public static int[] copyone(int n) {
     //System.out.println("执行copyone");
        int one[] = new int[n];
        System.out.println("请输入数组元素");
        for(int i=0;i<n;i++) {
            one[i] = s.nextInt();
        }
        return one;
    }
    
    public static int[][] copytwo(int m,int n){
        //System.out.println("执行coppytwo");
        int two[][]=new int[m][n];
        System.out.println("请输入数组元素");
        for(int i=0 ;i<m ;i++) {
            for(int j=0 ;j<n ;j++) {
                two[i][j]=s.nextInt();
            }
        }
        return two;
    }
}

  • 14
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值