重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用C++实现一个JAVA代码:
专注于为中小企业提供做网站、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业淇滨免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
第一步i采用Java实现,以下是部分代码。
package tree;import java.util.Scanner;public class Main { //根节点 private Student root = null; //输入器 private Scanner scanner = new Scanner(System.in); //业务 private StudentService ss = new StudentService(); /** * 开始 */ public void startService(){ System.out.println("欢迎使用宿舍管理系统"); while(true){ System.out.println("1,添加室友信息\t2,显示所有室友信息\t3,按学号查找室友\t4,退出"); System.out.print("请选择:"); int choose = Integer.parseInt(scanner.nextLine()); switch (choose) { case 1: this.addRoomMates(); break; case 2: printRoomMates(root); break; case 3: this.findRoomMatesBySno(); break; case 4: System.out.println("info -- 感谢使用,再见!"); System.exit(0); break; default: System.out.println("info -- 无此项,请重新选择!"); break; } } } /** * 按学号查找 */ private void findRoomMatesBySno() { while(true){ System.out.println("1,查找\t2,退到上一层"); System.out.print("请选择:"); int choose = Integer.parseInt(scanner.nextLine()); switch (choose) { case 1: find(); break; case 2: return; default: System.out.println("info -- 无此项,请重新选择!"); } } }对于学习有困难的看了也不知道怎么去做的可以加扣:五7八接着再来不要断开0二四后面跟着再来一4四连在一起即可,进行大神的交流同时得到帮助 /** * 找 */ private void find() { System.out.print("请输入学号:"); String sno = scanner.nextLine(); System.out.println("学号 \t 姓名"); ss.findRoomMatesBySno(root,sno); } //添加 private void addRoomMates() { while(true){ System.out.println("1,添加\t2,退到上一层"); System.out.print("请选择:"); int choose = Integer.parseInt(scanner.nextLine()); switch (choose) { case 1: add(); break; case 2: return; default: System.out.println("info -- 无此项,请重新选择!"); } } } private void add() { System.out.print("请输入学号:"); String sno = scanner.nextLine(); System.out.print("请输入姓名:"); String name = scanner.nextLine(); Student temp = ss.addRoomMates(root,new Student(sno,name)); if(root == null){ root = temp; } } public void printRoomMates(Student root) { System.out.println("学号 \t 姓名"); ss.printRoomMates(root); } public static void main(String[] args) { new Main().startService(); }}
很简单的应用,为了节省字数,代码注释我就不加了
首先是显示层,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);
}
}
不知道分层设计思想是不是我想的这样
package a;
import java.io.File;
import java.util.Scanner;
public class SearcFile {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("输入目录");
String dir=scanner.next();
File rootDir=new File(dir);
searchFile(rootDir,"");
}
private static void searchFile(File rootDir, String space) {
//space用来记录文件级别,子文件前面会多输出两个空格
if (rootDir.isDirectory()) {
//如果rootDir是文件夹,就输出目录名称,space加两个空格
System.out.println(space+"+--"+rootDir.getName());
space+=" ";
//在进入该文件夹继续查询
for (File subFile:rootDir.listFiles()) {
searchFile(subFile,space);
}
} else {
//如果rootDir是个文件,就直接输出文件名称
System.out.println(space+"--"+rootDir.getName());
}
}
}
ava要运行html代码,需要运行在服务器端,也就是servlet容器中,经过容器编译解析,返回html静态内容,示例如下:
在servlet里面写就可以了
引入一系列包
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class Servlet1 extends HttpServlet {
public void doGet(ServletRequest req,ServletResponse res)throws ServletException, IOException{try{PrintWriter pw=res.getWriter();//在浏览器输出需要
pw.println("scriptscript");}catch(exception e){="" e.printstacktrace();="" 为发现调试错误}}}=""
就是让人输入一些数字,然后让程序输出最小的数是哪个对吗?是的话回复一下,我追答给你
API中的内容:从类javax.swing.AbstractButton继承的方法doClickpublicvoiddoClick()以编程方式执行“单击”。此方法的效果等同于用户按下并随后释放按钮。doClickpublicvoiddoClick(intpressTime)以编程方式执行“单击”。此方法的效果等同于用户按下并随后释放按钮。按钮在虚拟“按下”状态下停留pressTime毫秒的时间。参数:pressTime-“按下”按钮的时间,以毫秒为单位