java代码在哪里编写(java怎么开始写代码)
大家好,关于java代码在哪里编写很多朋友都还不太明白,今天小编就来为大家分享关于java怎么开始写代码的知识,希望对各位有所帮助!
怎么写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代码
以下是在手机上写代码的步骤(以安卓手机为例):
1、在百度上搜索AIDE程序并下载安装好;
2、打开程序默认进入的JAVA源代码编写界面。系统自动给出了一段JAVA“hello world”源代码。点击右上角的播放器按钮即可编译运行。如果源代码有误编译器会提示错误地方,还是比较人性化的。成功编译则可以看到屏幕上打印出“hello world”两个单词;
3、根据自己需要可以选择编写其他代码就可以了;
以上是在手机上编写代码的步骤,感兴趣的可以试试!
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!