『壹』 求教大佬!java實現隨機出題
public class test {
public static void main(String[] args) {
// java實現隨機出題
// 50以內的加減乘除法,用戶解答後系統判斷對錯,用戶輸入-1時中止訓練
Scanner scan = new Scanner(System.in);
char[] arr = {'+','-','*','÷'};
while (true) {
// 生成一個隨機小數,范圍從 2.25 到 2.75的兩位小數
// double sum = Math.random()*(2.75-2.25)+2.25;
int a = (int) (Math.random()*(50-0)+0);
int b = (int) (Math.random()*(50-0)+0);
int f = (int) (Math.random()*(4-0)+0);
if (f== 1 && a < b) {
continue;
}
if (f== 3 && a == 0) {
continue;
}
System.out.println("題目:"+a+arr[f]+b+"=\n請輸入:");
double sum = 0;
if (f == 0) {
sum = a+b;
}
if (f == 1) {
sum = a-b;
}
if (f == 2) {
sum = a*b;
}
if (f == 3) {
sum = 1.0*a/b;
}
double s = scan.nextDouble();
if (s == -1) {
System.err.println("結束");
break;
}
if (s == sum) {
System.err.println("正確");
}else {
System.err.println("錯誤");
}
}
}
}
『貳』 用java編寫學生信息管理系統,只需有添加,刪除,修改,查詢,等功能即可!急需
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.sql.*;
import java.awt.GridLayout;
class myStudent {
public myStudent() {
}
public static void main(String[] args) {
loginFrame lf=new loginFrame();
lf.setVisible(true);
lf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
.exit(0);
}
});
}
}
class mainFrame extends JFrame implements ActionListener{
MenuBar myMenuBar=new MenuBar();
Menu myMenuFile,myMenuEdit,myMenuUser,myMenuAbout;
MenuItem miNew,miOpen,miSave,miSaveAs,miExit;
MenuItem miAdd,miEdit,miDel,miFind,miShow,miUser,miAbout;
public JTable table = new JTable();
Icon background_icon=new ImageIcon("pct.jpg");
JLabel background_Label=new JLabel(background_icon);
public DefaultTableModel mm ;
public mainFrame(){
myMenuFile=new Menu("文件");
miNew=new MenuItem("新建");
miOpen=new MenuItem("打開");
miSave=new MenuItem("保存");
miExit=new MenuItem("退出");
//miNew.enable(false);
//miOpen.enable(false);
//miSave.enable(false);
//myMenuFile.add(miNew);
//myMenuFile.add(miOpen);
//myMenuFile.add(miSave);
myMenuFile.add(miExit);
myMenuEdit=new Menu("數據編輯");
miFind=new MenuItem("查找數據");
miAdd=new MenuItem("添加數據");
miEdit=new MenuItem("修改數據");
miDel=new MenuItem("刪除數據");
miShow=new MenuItem("全部顯示");
myMenuEdit.add(miFind);
myMenuEdit.add(miAdd);
myMenuEdit.add(miEdit);
myMenuEdit.add(miDel);
myMenuEdit.add(miShow);
myMenuUser=new Menu("用戶");
miUser=new MenuItem("編輯用戶");
myMenuUser.add(miUser);
myMenuAbout=new Menu("我的課程設計");
miAbout=new MenuItem("課程設計說明");
myMenuAbout.add(miAbout);
myMenuBar.add(myMenuFile);
myMenuBar.add(myMenuEdit);
myMenuBar.add(myMenuUser);
myMenuBar.add(myMenuAbout);
String[] col = {"學號","姓名","性別","民族","年齡","年級","班級","專業"};//創建屬性列名
mm= new DefaultTableModel(col,0);
table.setModel(mm);
jscrollPane tableScrollPane = new JScrollPane(table);
this.setMenuBar(myMenuBar);
this.add(tableScrollPane);
miExit.addActionListener(this);
miFind.addActionListener(this);
miAdd.addActionListener(this);
miEdit.addActionListener(this);
miDel.addActionListener(this);
miShow.addActionListener(this);
miUser.addActionListener(this);
miAbout.addActionListener(this);
}
class myConnection{
ResultSet re;
String strurl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=學生管理資料庫.mdb";
public myConnection(){}
public ResultSet getResult(String sql){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(strurl);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet re=stmt.executeQuery(sql);
return re;
}
catch(Exception e){
System.out.println("getResult------"+e.toString());
return null;
}
}
class addFrame extends JDialog implements ActionListener{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
JLabel 學號1,姓名1,性別1,民族1,年齡1,年級1,班級1,專業1;
JTextField 學號,姓名,性別,民族,年齡,年級,班級,專業;
JButton b;
JPanel p;
mainFrame mf;
class editFrame extends JDialog implements ActionListener{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
JLabel 學號1,姓名1,性別1,民族1,年齡1,年級1,班級1,專業1;
JTextField 學號,姓名,性別,民族,年齡,年級,班級,專業;
JButton b;
JPanel p;
mainFrame mf;
class findFrame extends JDialog implements ActionListener{
mainFrame mf;
JPanel p;
JComboBox c;
JTextField t;
JButton b;
JButton fAll;
String sql="select * from student";
String[] colStr={"學號","姓名","性別","民族","年齡","年級","班級","專業"};
public findFrame(mainFrame mmf){
mf=mmf;
p=new JPanel();
c=new JComboBox(colStr);
t=new JTextField(10);
b=new JButton("查找");
fAll=new JButton("全部顯示");
b.addActionListener(this);
fAll.addActionListener(this);
p.add(new JLabel("選擇"));
p.add(c);
p.add(new JLabel("查找內容"));
p.add(t);
p.add(b);
p.add(fAll);
this.add(p);
this.setTitle("查找");
this.setSize(450,80);
}
class aboutFrame extends JDialog{
aboutFrame(){
JPanel p1=new JPanel();
p1.add(new JLabel("學生信息管理系統"));
p1.add(new JLabel("白金曉 201006040203"));
p1.add(new JLabel("承德石油高等專科學校"));
p1.add(new JLabel("計算機類1002 "));
p1.setLayout(new GridLayout(4,1,5,5));
this.add(p1);
this.setTitle("關於我");
this.setSize(300,200);
}
}
class userFrame extends JFrame implements ActionListener{
JTextField user,pass;
JButton add,del;
JTable t;
JPanel p1,p2,p3,p4,p5;
DefaultTableModel m;
public userFrame(){
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
user=new JTextField(8);
pass=new JTextField(8);
add=new JButton("添加");
del=new JButton("刪除");
String[] col = {"用戶名","密碼"};
m= new DefaultTableModel(col,0);
t=new JTable();
t.setModel(m);
JScrollPane sp = new JScrollPane(t);
p1.add(new JLabel("用戶名"));
p1.add(user);
p1.add(new JLabel("密碼"));
p1.add(pass);
p1.add(add);
p2.add(sp);
p3.add(del);
add.addActionListener(this);
del.addActionListener(this);
myConnection conn=new myConnection();
ResultSet rs;
rs=conn.getResult("select * from 管理員");
if (rs!=null){
try{
//m.setRowCount(0);
//table.setModel(mm);
while(rs.next()){
String 用戶名 = rs.getString("用戶名");
String 密碼 = rs.getString("密碼");
String[] cloumns ={用戶名,密碼};
m.addRow(cloumns);
}
t.setModel(m);
}catch(Exception e){
System.out.println(e.toString());
}
}
this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);
this.add(p3,BorderLayout.SOUTH);
this.add(p4,BorderLayout.WEST);
this.add(p5,BorderLayout.EAST);
this.setTitle("用戶管理");
this.setSize(600,400);
}
class loginFrame extends JDialog implements ActionListener{
JPanel p;
JTextField user,pass;
JButton login,cancel;
public loginFrame(){
p=new JPanel();
user=new JTextField(10);
pass=new JTextField(10);
login=new JButton("登錄");
cancel=new JButton("退出");
login.addActionListener(this);
cancel.addActionListener(this);
Icon background_icon=new ImageIcon("pct.jpg");
JLabel background_Label=new JLabel(background_icon);
p.add(background_Label);
p.add(new JLabel("賬號"));
p.add(user);
p.add(new JLabel("密碼"));
p.add(pass);
p.add(login);
p.add(cancel);
this.add(p);
this.setTitle("系統登錄");
this.setSize(360,260);
}
『叄』 如何用Java實現隨機出題
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import oracle.jdbc.driver.OracleDriver;
public class GenPaperServlet extends HttpServlet
{
Connection conn;
Statement stmt;
ResultSet rs;
int total_question_num;
int total_question_in_paper;
int total_paper_num;
String curr_classid;
public GenPaperServlet()
{
conn = null;
stmt = null;
rs = null;
total_question_num = 0;
total_question_in_paper = 0;
total_paper_num = 0;
curr_classid = "";
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html;charset=GBK");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println("<html><head></head><body><center>");
printwriter.println("請以POST方式提交");
printwriter.println("</center></body></html>");
printwriter.close();
}
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html;charset=GBK");
PrintWriter printwriter = httpservletresponse.getWriter();
String s = httpservletrequest.getParameter("classid"); //"20"
String s1 = httpservletrequest.getParameter("paper_num"); //"1"
if(s == null || s1 == null)
{
printwriter.println("<center>");
printwriter.println("請按照正常方式提交數據<br>");
printwriter.println("<a href=/test/admin/genpaper.jsp>單擊這里設置生成試卷的參數</a>");
printwriter.println("</center>");
}
total_paper_num = Integer.parseInt(s1);
curr_classid = s;
int i = 0;
if(!open_db(curr_classid))
{
printwriter.println("打開資料庫錯誤!");
return;
}
if(!setParams(curr_classid))
{
System.out.println("設置系統參數錯誤!");
return;
}
if(!verify_QuertionLib())
{
printwriter.println("試題庫中試卷不足,請增加新的試題!");
printwriter.println("班級代號:" + curr_classid);
printwriter.println("該班級一套試卷中的試題數:" + total_question_in_paper);
printwriter.println("目前題庫中該班級的試題總數:" + total_question_num);
return;
}
i = genPaper(total_paper_num, curr_classid);
if(i == 0)
{
printwriter.println("生成試卷操作失敗!");
return;
}
if(!updateOtherTable(i, curr_classid))
{
printwriter.println("更新相關表操作失敗!");
return;
} else
{
printwriter.println("<center>");
printwriter.println("動態組捲成功!<br>");
printwriter.println("共生成了 " + i + " 套試卷<br>");
printwriter.println("<a href=/test/admin/genpaper.jsp>單擊這里設置生成試卷的參數</a>");
printwriter.println("</center>");
return;
}
}
public boolean open_db(String s)
{
try
{
new OracleDriver();
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora9", "scott", "tiger");
stmt = conn.createStatement();
}
catch(Exception exception)
{
return false;
}
return true;
}
public boolean setParams(String s)
{
String s1 = "";
try
{
String s2 = "select count(questionid) as countquestionid from test_question_lib ";
s2 = s2 + "where classid='" + s + "'";
rs = stmt.executeQuery(s2);
rs.next();
total_question_num = rs.getInt("countquestionid");
s2 = "select totalques from test_classinfo ";
s2 = s2 + "where classid='" + s + "'";
rs = stmt.executeQuery(s2);
rs.next();
total_question_in_paper = rs.getInt("totalques");
}
catch(Exception exception)
{
return false;
}
return true;
}
public boolean verify_QuertionLib()
{
return total_question_num >= total_question_in_paper;
}
public boolean updateOtherTable(int i, String s)
{
int j = 0;
String s1 = "update test_classinfo set totalpaper=totalpaper+";
s1 = s1 + i + " where classid='" + s + "'";
try
{
j = stmt.executeUpdate(s1);
}
catch(Exception exception)
{
return false;
}
return j == 1;
}
public int genPaper(int i, String s)
{
boolean flag = false;
boolean flag1 = false;
boolean flag2 = false;
boolean flag3 = false;
String s1 = "";
try
{
int ai[] = new int[total_question_num];
int i1 = 0;
boolean flag4 = false;
String s2 = "select max(paper_id) as max_paper_id from test_paper_lib";
rs = stmt.executeQuery(s2);
rs.next();
int j = rs.getInt("max_paper_id") + 1;
s2 = "select questionid from test_question_lib where classid='" + s + "'";
for(rs = stmt.executeQuery(s2); rs.next();)
ai[i1++] = rs.getInt("questionid");
for(int k1 = 0; k1 < i; k1++)
{
int k = ai.length; //8
for(int l1 = 0; l1 < total_question_in_paper; l1++)
{
// int ai[] ={1 ,3 , 5 ,9 , 56,30 96 ,25};
int j1 = (int)Math.floor(Math.random() * (double)k); // 4
String s3 = "insert into test_paper_lib values(";
s3 = s3 + "test_seq_paper.nextval," + j + "," + ai[j1] + ")";
stmt.executeUpdate(s3);
int l = ai[k - 1];
ai[k - 1] = ai[j1];
ai[j1] = l;
k--;
}
j++;
}
}
catch(Exception exception)
{
return 0;
}
return i;
}
}
『肆』 《Java》期末課程設計
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class QuestionOne {
/**
* 編程將從鍵盤輸入文本中的子字元串「word」替換為字元串「world」, 並刪除所有的子字元串「this」。
* 序程要求:程序中要包含局遲有注釋,對於使用的變數和方法的功能要加以說明。
*/
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String message = null; // 存儲用戶輸瞎臘友入的字元磨槐串
try {
while ((message = reader.readLine()) != null) {
// 列印處理前的字元串
System.out.println("輸入的字元串為:" + message);
// 把 word 替換為 world
message = message.replaceAll("word", "world");
// 把 this 替換為 空
message = message.replaceAll("this", "");
// 列印處理後的字元串
System.out.println("處理後為:" + message);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("出現異常,程序退出!");
}
}
}