public class test1 {
武城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入本月税前工资");
double d = scan.nextDouble(); //税前工资
double sal= d - 3500; // 减去扣除数的
double salary = 0; //初始化工资
if(sal=1500){
salary = sal*0.03;
}else if(sal=4500){
salary = sal*0.1-105;
}else if(sal=9000){
salary = sal*0.2-555;
}else if(sal=35000){
salary = sal*0.25-1005;
}else if(sal=55000){
salary = sal*0.3-2755;
}else if(sal=80000){
salary = sal*0.35-5505;
}else{
salary = sal*0.45-13505;
}
System.out.println("个人所得税的缴纳额度为:"+salary+"元");
}
}
尽量简化逻辑 像钱的多少 先用变量存储起来 再带入就好了
尽量简化代码 像输出也只用一句然后带入就好了
尽量自己考虑 每一点问题都是自己敲代码的过程中产生然后明白的
这题是我原题 我知道你在哪 既然在那了 为什么还不努力呢
import java.util.Date;
public class Travel{
TravelLeg mid = new TravelLeg();
private String start;
private String end;
private Date startTime;
private Date endTime;
private String travelId;
private String comment;
public void add(TravelLeg mid){
this.start = mid.getStart();
this.mid = mid;
this.end = mid.getEnd();
}
public boolean check_date(){
if(!mid.getStartTime().before(mid.getEndTime())){
System.out.println("时间错误");
return false;
}else return true;
}
public void print(){
System.out.println("开始地点:"+this.start+"中间地点"+this.mid.getEnd()+this.end);
}
}
import java.util.Scanner;
import demo.ui.MainShell;
import demo.ui.PublicStatic;
public class DemoApp {
public static int getScanner() {
int input = 0;
try {
Scanner scanner = new Scanner(System.in);
input = scanner.nextInt();
} catch (Exception e) {
System.out.println("\t\t请重新输入");
return getScanner();
}
return input;
}
public static void main(String[] args) {
System.out.println("\t\t\t欢迎使用系统");
System.out.println("\t\t***********************");
System.out.println("\t\t\t1、客户信息管理");
System.out.println("\t\t\t2、购物结算");
System.out.println("\t\t\t3、真情回馈");
System.out.println("\t\t\t4、注销");
System.out.println("\t\t***********************");
while(true) {
int input = getScanner();
switch (input) {
case 1:
System.out.println("\t\t\t1、客户信息管理");
break;
case 2:
System.out.println("\t\t\t2、购物结算");
break;
case 3:
System.out.println("\t\t\t3、真情回馈");
break;
case 4:
System.out.println("\t\t\t4、注销");
break;
default:
System.out.println("\t\t请重新输入");
break;
}
if(input == 4) {
break;
}
}
}
}
很简单的应用,为了节省字数,代码注释我就不加了
首先是显示层,LoinWindow:
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class LoinWindow extends JFrame implements ActionListener, FocusListener {
private JPanel mainPanel, namePanel, btnPanel;
private JTextField tfName, tfPsd;
private JButton btnLogin, btnCancel;
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
private LoginService service = new LoginService();
public LoinWindow() {
super("登录窗体");
}
public void launch() {
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout mainLayout = new GridLayout(2, 1);
mainLayout.setVgap(10);
mainPanel = new JPanel(mainLayout);
GridBagLayout nameLayout = new GridBagLayout();
namePanel = new JPanel(nameLayout);
namePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel nameLabel = new JLabel("姓名:");
tfName = new JTextField();
JLabel psdLabel = new JLabel("密码:");
tfPsd = new JTextField();
JLabel blank = new JLabel(" ");
namePanel.add(nameLabel);
namePanel.add(tfName);
namePanel.add(blank);
namePanel.add(psdLabel);
namePanel.add(tfPsd);
GridBagConstraints s = new GridBagConstraints();
s.fill = GridBagConstraints.BOTH;
s.gridwidth = 1;
s.weightx = 0;
s.weighty = 0;
nameLayout.setConstraints(nameLabel, s);
s.gridwidth = 0;
s.weightx = 1;
s.weighty = 0;
nameLayout.setConstraints(tfName, s);
s.gridwidth = 0;
s.weightx = 4;
s.weighty = 0;
nameLayout.setConstraints(blank, s);
s.gridwidth = 1;
s.weightx = 0;
s.weighty = 0;
nameLayout.setConstraints(psdLabel, s);
s.gridwidth = 3;
s.weightx = 1;
s.weighty = 0;
nameLayout.setConstraints(tfPsd, s);
FlowLayout btnLayout = new FlowLayout();
btnLayout.setAlignment(FlowLayout.CENTER);
btnPanel = new JPanel(btnLayout);
btnLogin = new JButton("确定");
btnCancel = new JButton("取消");
btnPanel.add(btnLogin);
btnPanel.add(btnCancel);
btnCancel.addActionListener(this);
btnLogin.addActionListener(this);
mainPanel.add(namePanel);
mainPanel.add(btnPanel);
setContentPane(mainPanel);
tfName.addFocusListener(this);
tfPsd.addFocusListener(this);
pack();
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == btnCancel) {
System.exit(0);
} else if(source == btnLogin) {
String username = tfName.getText();
String password = tfPsd.getText();
boolean success = service.login(username, password);
if(success) {
warn("成功", "登录成功!");
} else {
warn("失败", "您输入的用户名或密码错误 !");
}
}
}
@Override
public void focusGained(FocusEvent arg0) {
}
@Override
public void focusLost(FocusEvent e) {
Object source = e.getSource();
if(source == tfName) {
String username = tfName.getText();
try {
service.matchUsername(username);
} catch (LoginException e1) {
warn("验证错误", e1.getMessage());
}
} else if(source == tfPsd) {
String password = tfPsd.getText();
try {
service.matchPassword(password);
} catch (LoginException e1) {
warn("验证错误", e1.getMessage());
}
}
}
private void warn(String title, String msg) {
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new LoinWindow().launch();
}
}
然后是模型层:LoginDao
public class LoginDao {
public boolean login(String username, String password) {
if(username.equals("admin") password.equals("12345")) {
return true;
}
return false;
}
}
LoginService
import java.util.regex.Pattern;
public class LoginService {
private static final Pattern LOGIN_PATTERN = Pattern.compile("[a-zA-Z]+");
private static final Pattern PASSWORD_PATTERN = Pattern.compile("[1-9]+");
private LoginDao dao = new LoginDao();
public boolean matchUsername(String username) throws LoginException {
if(null == username || username.isEmpty()) {
return false;
}
if(!LOGIN_PATTERN.matcher(username).matches()) {
throw new LoginException("您输入的用户名不合法,请输入英文!");
}
return true;
}
public boolean matchPassword(String password) throws LoginException {
if(null == password || password.isEmpty()) {
return false;
}
if(!PASSWORD_PATTERN.matcher(password).matches()) {
throw new LoginException("您输入的密码不合法,请输入数字!");
}
return true;
}
public boolean login(String username, String password) {
if(null == username || username.isEmpty()) {
return false;
}
if(null == password || password.isEmpty()) {
return false;
}
if(!dao.login(username, password)) {
return false;
}
return true;
}
}
LoginException
public class LoginException extends Exception {
public LoginException(String arg0) {
super(arg0);
}
}
不知道分层设计思想是不是我想的这样