类的实例化与方法调用

实验目的:1.掌握定义类、创建对象、使用类与对象。

2.掌握类及成员的修饰符的使用。

3.掌握构造函数的使用。

4.掌握如何定义方法和调用方法。

5.掌握形式参数与实际参数的结合过程。

实验步骤与内容:

1.编写一个Application程序MyDate.java,建立日期类,接受用户输入的年、月、日,完成日期增加若干天得到新的日期,日期推前若干天得到新的日期,日期与日期间相差多少天的计算。

2.编写一个彩票中奖模拟程序,实现下述功能:创建六个筛子,每个筛子可以定制有多少个面(一般6-30);投掷六个筛子,得出六个开奖数值;模拟若干参与者,进行赌采,根据开奖数值,给出猜对的成绩。

主要代码:
第一题:
import java.util.Scanner;
public class DateTest {
 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  int y, m, d;
  System.out.println("请输入年、月、日:");
  y = scan.nextInt();
  m = scan.nextInt();
  d = scan.nextInt();
  Date my_date = new Date(y, m, d);
  System.out.println("当前日期是:" + my_date);
  System.out.println("请输入要增加的天数:");
  int days = scan.nextInt();
  my_date.plusDay(days);
  System.out.println("增加后的日期是:" + my_date);
  System.out.println("请输入要减少的天数:");
  days = scan.nextInt();
  my_date.minusDay(days);
  System.out.println("减去后的日期是:" + my_date);
  System.out.println("请输入要比较的日期的年月日:");
  y = scan.nextInt();
  m = scan.nextInt();
  d = scan.nextInt();
  Date my_date2 = new Date(y, m, d);
  System.out.println("两日期相差天数是:" + my_date.differDay(my_date2));
 }
}
class Date {
 private int year;
 private int month;
 private int day;
 public Date(int year, int month, int day){
  this.year = year;
  this.month = month;
  this.day = day;
 }
 public void setYear(int year) {
  this.year = year;
 }
 public int getYear(){
  return year;
 }
 public void setMonth(int month) {
  this.month = month;
 }
 public int getMonth() {
  return month;
 }
 public void setDay(int day) {
  this.day = day;
 }
 public int getDay() {
  return day;
 }
 public String toString() {
  return year + "年" + month + "月" + day + "日";
 }
 public int dayOfMonth(int m) {
  switch(m) {
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
    return 31;
   case 4:
   case 6:
   case 9:
   case 11:
    return 30;
   case 2:
    if((year%4 == 0 && year%4 != 0) || (year%400 == 0))
     return 29;
    else
     return 28;
  }
  return -1;
 }
 public void plusDay(int d) {
  if(day+d <= dayOfMonth(month)) {
   day += d;
  }else {
   d -= dayOfMonth(month) - day + 1;
   day = 1;
   if(month < 12) {
    month++;
   }else {
    year++;
    month = 1;
   }
   plusDay(d); 
  }
 }
 public void minusDay(int d) {
  if(day-d > 0) {
   day -= d;
  }else {
   d -= day;
   if(month > 1) {
    month--;
   }else {
    month = 12;
    year--;
   }
   day = dayOfMonth(month);
   minusDay(d);
  }
 }
 public int dayOfDate(Date date) {
  int days = 0;
  int y = date.getYear();
  int m = date.getMonth();
  int d = date.getDay();
  days = 365*(y-1)+(y-1)/4-(y-1)/100+(y-1)/400;
  for(int i=1; i<m; i++)
   days += dayOfMonth(m);
  days += d;
  return days;
 }
 public int differDay(Date date) {
  int days1 = dayOfDate(this);
  int days2 = dayOfDate(date);
  return (days1>days2) ? (days1-days2) : (days2-days1);
 }
}

第二题:

import java.util.Random;
import java.lang.Math;
import java.util.Scanner;
public class DieTest {
 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  Die[] die = new Die[6];
  int number;
  for(int i=0; i<6; i++) {
   die[i] = new Die(6);
   System.out.print("请输入你猜的数值:");
   number = scan.nextInt();
   die[i].guess(number);
  }
 }
}
class Die {
 private int face;
 private int value;
 public Die(int face) {
  this.face = face;
  value = (int)(Math.random() * face) + 1;
 }
 public int getValue() {
  return value;
 }
 public void setValue(int v) {
  value = v;
 }
 public void guess(int v) {
  if(v == value)
   System.out.println("恭喜你,猜中了!");
  else
   System.out.println("很遗憾,你没有猜中");
 }
}
### 使用 `ON DUPLICATE KEY UPDATE` 更新所有字段但排除主键MySQL 中,当使用 `ON DUPLICATE KEY UPDATE` 语句时,如果希望更新主键外的所有字段,可以通过显式列出需要更新的字段来实现。由于主键通常设置为 `AUTO_INCREMENT` 具有唯一性约束,因此在插入更新操作中不应直接修改主键字段。 #### 示例 假设有一个名为 `employees` 的表,定义如下: ```sql CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), department VARCHAR(50), salary DECIMAL(10, 2) ); ``` 现在需要插入一条记录,如果 `id` 冲突(例如,表中已存在相同的主键),则更新主键外的所有字段: ```sql INSERT INTO employees (id, name, department, salary) VALUES (101, 'Alice', 'Engineering', 85000.00) ON DUPLICATE KEY UPDATE name = 'Alice', department = 'Engineering', salary = 85000.00; ``` 在这个例子中,如果 `id = 101` 的记录已经存在,则会更新 `name`、`department` 和 `salary` 字段;如果不存在,则插入新记录。主键 `id` 在插入时指定,但在更新时不被修改 [^4]。 #### 使用 `VALUES()` 函数简化更新语句 当插入的数据较多时,可以使用 `VALUES()` 函数来引用插入时的值,从而简化更新语句: ```sql INSERT INTO employees (id, name, department, salary) VALUES (101, 'Alice', 'Engineering', 85000.00), (102, 'Bob', 'Marketing', 75000.00) ON DUPLICATE KEY UPDATE name = VALUES(name), department = VALUES(department), salary = VALUES(salary); ``` 在这个例子中,如果 `id` 冲突,`ON DUPLICATE KEY UPDATE` 子句会使用当前插入的值来更新对应的 `name`、`department` 和 `salary` 字段 [^4]。 #### 注意事项 - `ON DUPLICATE KEY UPDATE` 需要表中存在主键唯一索引,否则不会触发更新操作 [^1]。 - 如果更新的字段包含主键唯一索引列,并且更新后的值与其他记录冲突,会导致插入和更新都失败。 - 使用 `ON DUPLICATE KEY UPDATE` 时,自增字段(`AUTO_INCREMENT`)可能会跳过某些值,因为每次插入操作都会尝试分配一个新的自增值,即使最终执的是更新操作 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值