java怎么写代码,java程序的代码实现
老铁们,大家好,相信还有很多朋友对于java怎么写代码和java程序的代码实现的相关问题不太懂,没关系,今天就由我来为大家分享分享java怎么写代码以及java程序的代码实现的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!
java 代码怎么写
package com.date;
public class DateDome{
private int year=0;//年
private int month=0;//月
private int day=0;//日
public DateDome(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public DateDome(){
}
//年大于等于0
public boolean isYear(){
boolean suc=false;
if(year<0)return suc;
else if(year>=0)suc=true;
return suc;
}
//判断月数1-12月
public boolean isMonth(){
boolean suc=false;
if(month<0||month>12)return false;
else suc=true;
return suc;
}
//判断天数1-31号
public boolean isDay(){
boolean suc=false;
if(day<=0||day>31)return suc;
else suc=true;
return suc;
}
//是否为闰年
public boolean isRunNian(int year){
boolean suc=false;
if(year>=0){
if(year%400==0||(year%4==0&&year%100!=0)){
suc=true;
}
}
return suc;
}
//判断合法年月日
public boolean isInvaildate(int year,int month, int day){
boolean suc=false;
if(isYear()&&isMonth()&&isDay()){
switch(month){
case 1:
suc=true;
break;
case 2:
if(isRunNian(year)&&day<=29){
suc=true;
}else if(day<=28){
suc=true;
}
break;
case 3:
suc=true;
break;
case 4:
if(day<=30)suc=true;
break;
case 5:
suc=true;
break;
case 6:
if(day<=30)suc=true;
break;
case 7:
suc=true;
break;
case 8:
suc=true;
break;
case 9:
if(day<=30)suc=true;
break;
case 10:
suc=true;
break;
case 11:
if(day<=30)suc=true;
break;
case 12:
suc=true;
break;
}
}
return suc;
}
//根据日期得到天数
public int getDaysByDate(int year,int month,int day){
int days=0;
if(isInvaildate(year,month,day)){
for(int i=0;i<year;i++){
if(isRunNian(i)){
days+=366;
}else{
days+=365;
}
}
for(int i=1;i<month;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
days+=31;
}else if(i==4||i==6||i==9||i==11){
days+=30;
}else if(i==2){
if(isRunNian(year)){
days+=29;
}else{
days+=28;
}
}
}
days+=day-1;
return days;
}else{
System.out.println("处理有非法日期!!!");
return-1;
}
}
//根据天数得到日期数int[]由,年、月、日组成的数组
public int[] getDateByDays(int days){
int das=0;//预计的天数
int y=0,m=1,d=1;//0年1月1号
int[] date=new int[3];
boolean suc=true;
int temp=0;
if(days<0){
System.out.println("请输入合法天数!!!");
return new int[]{0,0,0};
}
while(suc){
if(isRunNian(y)){
temp=366;
}else{
temp=365;
}
das+=temp;
if(das<days){
y++;
}else{
das-=temp;
break;
}
}
while(suc){
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
temp=31;
}else if(m==4||m==6||m==9||m==11){
temp=30;
}else if(m==2){
if(isRunNian(y)){
temp=29;
}else{
temp=28;
}
}
das+=temp;
if(das<days){
m++;
}else{
das-=temp;
break;
}
}
d=days-das+1;
date[0]=y;
date[1]=m;
date[2]=d;
return date;
}
//得到多少天前或后合法日期
public int[] addORsubDay(int dd){
int days=getDaysByDate(year,month,day);
if(days>=0){
days+=dd;
if(days>0){
return getDateByDays(days);
}else{
System.out.println("处理日期不能小于0年1月1号");
return new int[]{0,0,0};//代表无效日期
}
}else{
System.out.println("处理日期不能小于0年1月1号");
return new int[]{0,0,0};//年,月,日
}
}
//得到两个日期相距天数
public int TwoDate(int[] date1,int[] date2){
int d=-1;
if(isInvaildate(date1[0],date1[1],date1[2])&&isInvaildate(date2[0],date2[1],date2[2])){
int days1=getDaysByDate(date1[0],date1[1],date1[2]);
int days2=getDaysByDate(date2[0],date2[1],date2[2]);
d=days1-days2;
return d>=0?d:-d;
}else
{
System.out.println("处理有非法日期!!!");
return d;
}
}
}
我写了你提出的功能,你还可以扩展其它功能。
怎么写Java的代码
public class Reader{
private static int nextId= 10001;
private int readerId;
private String name;
private String password;
private double balance;
private String membership;
private double discountRate;
private int points;
public Reader(String name, String password, double balance, String membership, double discountRate){
this.readerId= nextId++;
this.name= name;
this.password= password;
this.balance= balance;
this.membership= membership;
this.discountRate= discountRate;
this.points= 0;
}
public int getReaderId(){
return readerId;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public double getBalance(){
return balance;
}
public String getMembership(){
return membership;
}
public double getDiscountRate(){
return discountRate;
}
public int getPoints(){
return points;
}
public void setDiscountRate(double discountRate){
this.discountRate= discountRate;
}
public void addPoints(int pointsToAdd){
this.points+= pointsToAdd;
}
public void deductBalance(double amount){
if(amount<= balance){
balance-= amount;
System.out.println("Successfully deducted"+ amount+" from balance.");
} else{
System.out.println("Insufficient balance.");
}
}
}
-成员变量:
- `readerId`:读者编号,是一个自动生成的流水号。
- `name`:读者姓名。
- `password`:读者密码。
- `balance`:账户余额。
- `membership`:身份等级,可以是"百通云员"或"非会员"。
- `discountRate`:折扣率。
- `points`:积分。
-构造函数:
-构造函数接受姓名、密码、账户余额、身份等级和折扣率作为参数,并自动分配读者编号。
-方法:
- `getReaderId()`:返回读者编号。
- `getName()`:返回读者姓名。
- `getPassword()`:返回读者密码。
- `getBalance()`:返回账户余额。
- `getMembership()`:返回身份等级。
- `getDiscountRate()`:返回折扣率。
- `getPoints()`:返回积分。
- `setDiscountRate(double discountRate)`:设置折扣率。
- `addPoints(int pointsToAdd)`:增加积分。
- `deductBalance(double amount)`:从余额中扣除指定金额。
怎么写Java代码
public class Reader{
private static int nextId= 10001;//初始编号
private int id;//读者编号
private String name;//姓名
private String password;//密码
private double balance;//账户余额
private String level;//身份等级
private double discountRate;//折扣率
private int points;//积分
public Reader(String name, String password, double balance, String level){
this.id= nextId++;
this.name= name;
this.password= password;
this.balance= balance;
this.level= level;
//设置折扣率
if(level.equals("VIP")){
this.discountRate= 0.8;
} else if(level.equals("普通会员")){
this.discountRate= 0.9;
} else{
this.discountRate= 1.0;
}
}
//提供 getter和 setter方法
public int getId(){
return id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name= name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password= password;
}
public double getBalance(){
return balance;
}
public void setBalance(double balance){
this.balance= balance;
}
public String getLevel(){
return level;
}
public void setLevel(String level){
this.level= level;
}
public double getDiscountRate(){
return discountRate;
}
public void setDiscountRate(double discountRate){
this.discountRate= discountRate;
}
public int getPoints(){
return points;
}
public void setPoints(int points){
this.points= points;
}
}
以上代码定义了一个`Reader`类,包含读者的相关信息作为成员变量,并提供了相应的 getter和 setter方法。在构造函数中,默认将读者编号设置为自增的流水号,并根据身份等级设置相应的折扣率。
文章到此结束,如果本次分享的java怎么写代码和java程序的代码实现的问题解决了您的问题,那么我们由衷的感到高兴!