1,读取 试题文件 然后做题算分

        File file1=new File("D:\\file","test.txt");
try{
FileReader in1=new FileReader(file1);
BufferedReader in2=new BufferedReader(in1);
String s;
int count=0;
for(;(s=in2.readLine())!=null;){
if(!s.startsWith("-")){
System.out.println(s);
}
else{
System.out.print("input your answer: ");
s=s.replaceAll("-","");
String daan;
Scanner scanner1=new Scanner(System.in);
daan=scanner1.next();
if(s.equals(daan)){
count++;
}
}
}
System.out.println("point "+count);
}
catch(Exception e){
System.out.println(e.getMessage());
}


2,用卡片布局做两个页面,来输入和输出

package testWin;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.CardLayout; public class TestWin implements ActionListener {
//使用卡片式布局,由菜单栏调用两个页面
private JFrame frame;
File file1;
JMenuItem input = new JMenuItem("input");
JMenuItem show = new JMenuItem("show");
InputArea inputMessage;
CardLayout card;
JPanel panel;
JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestWin window = new TestWin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the application.
*/
public TestWin() {
initialize();
} /**
* Initialize the contents of the frame.
*/
private void initialize() {
file1=new File("D:\\file","test.txt");
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar); JMenu menu = new JMenu("\u83DC\u5355");
menuBar.add(menu); menu.add(input); menu.add(show); input.addActionListener(this);
show.addActionListener(this); textArea=new JTextArea(12,50);
inputMessage=new InputArea(file1);
card=new CardLayout();
panel=new JPanel();
panel.setLayout(card);
panel.add("input",inputMessage);
panel.add("show",textArea);
frame.add(panel);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getSource()==input){
card.show(panel, "input");
}
else if(e.getSource()==show){
int number=1;
textArea.setText(null);
card.show(panel, "show");
try{
RandomAccessFile in=new RandomAccessFile(file1,"r");
String name=null;
for(;(name=in.readUTF())!=null;){
textArea.append("\n"+"name :"+name);
textArea.append("\t"+in.readUTF());
textArea.append("\t"+in.readUTF());
textArea.append("\n----------------------------------------------------------------------------");
number++;
} in.close();
}
catch(Exception e1){
System.out.println(e1.getMessage());
System.out.println("2222");
}
}
} }
package testWin;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import javax.swing.*;
import javax.swing.BoxLayout;
import javax.swing.JLabel; public class InputArea extends JPanel implements ActionListener{ /**
* Create the panel.
*/
File file1;
RandomAccessFile out1;//运用盒式布局,然后用随机流输入到文件
Box baseBox,box1,box2;
JButton button1;
private JLabel lblNewLabel;
private JLabel lblNewLabel_1;
private JLabel lblNewLabel_2;
private JLabel lblNewLabel_3;
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JButton button;
public InputArea(File f) {
setForeground(Color.CYAN);
this.file1=f;
baseBox=Box.createHorizontalBox();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
baseBox.add(box1); lblNewLabel = new JLabel("\u8F93\u5165\u59D3\u540D");
box1.add(lblNewLabel); lblNewLabel_1 = new JLabel("\u8F93\u5165qq");
box1.add(lblNewLabel_1); lblNewLabel_2 = new JLabel("\u8F93\u5165\u7535\u8BDD");
box1.add(lblNewLabel_2); lblNewLabel_3 = new JLabel("\u5355\u51FB\u5F55\u5165");
box1.add(lblNewLabel_3);
baseBox.add(box2); text1 = new JTextField();
box2.add(text1);
text1.setColumns(10); text2 = new JTextField();
box2.add(text2);
text2.setColumns(10); text3 = new JTextField();
box2.add(text3);
text3.setColumns(10); button = new JButton("\u5F55\u5165");
box2.add(button);
add(baseBox);
button.addActionListener(this); }
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
try{
RandomAccessFile out1=new RandomAccessFile(file1,"rw");
long length=file1.length();
out1.seek(length);
out1.writeUTF("姓名 : "+text1.getText());
out1.writeUTF("qq : "+text2.getText());
out1.writeUTF("电话 : "+text3.getText());
out1.close();
text1.setText(null);
text2.setText(null);
text3.setText(null);
}
catch(Exception e1){
System.out.println(e1.getMessage());
}
} }

3,编译器(编译可以,链接有bug

主类

package testWin;

import java.awt.BorderLayout;
import java.awt.EventQueue; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField; public class TestFrame extends JFrame implements ActionListener{ private JPanel contentPane;
private JTextField text1;
private JTextField text2;
JButton button1 = new JButton("\u7528\u8BB0\u4E8B\u672C\u7F16\u8F91\u6E90\u6587\u4EF6");
JButton button2 = new JButton("\u7F16\u8BD1");
JButton button3 = new JButton("\u8FD0\u884C");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 869, 111);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); contentPane.add(button1); JLabel label = new JLabel(" \u8F93\u5165\u6587\u4EF6\u540D \uFF1A");
contentPane.add(label); text1 = new JTextField();
contentPane.add(text1);
text1.setColumns(15); contentPane.add(button2); JLabel label_1 = new JLabel(" \u8F93\u5165\u4E3B\u7C7B\u540D\uFF1A");
contentPane.add(label_1); text2 = new JTextField();
contentPane.add(text2);
text2.setColumns(13); contentPane.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getSource()==button1){
System.out.println("dd");
Runtime ce=Runtime.getRuntime();
File file1=new File("D:\\javaWorkSpace\\Test\\src\\test22","test2.java");
// File file1=new File("D:\\file","test.java");
try{
ce.exec(file1.getAbsolutePath());
}
catch(Exception e1){
System.out.println(e1.getMessage());
}
}
else if(e.getSource()==button2){
CompileDialog compileDialog=new CompileDialog();
String name=text1.getText();
compileDialog.compile(name);
compileDialog.setVisible(true);
}
else if(e.getSource()==button3){
RunDialog runDialog=new RunDialog();
String name=text2.getText();
runDialog.run(name);
runDialog.setVisible(true);
}
} }

编译类

package testWin;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea; public class CompileDialog extends JDialog { private final JPanel contentPanel = new JPanel();
JTextArea textArea = new JTextArea(14,70);
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
CompileDialog dialog = new CompileDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the dialog.
*/
public CompileDialog() {
setBounds(100, 100, 871, 502);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane);
{
scrollPane.setViewportView(textArea);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
} public void compile(String name){
try{
Runtime ce=Runtime.getRuntime();
Process proccess =ce.exec("javac "+name);
InputStream in1=proccess.getErrorStream();
BufferedInputStream in2=new BufferedInputStream(in1);
int n;
boolean bn=true;
byte error[]=new byte[100];
for(;(n=in2.read(error,0,100))!=-1;){
String s=null;
s=new String(error,0,n);
textArea.append(s);
if(s!=null)bn=false;
}
if(bn)
textArea.append("编译正确");
}
catch(IOException e2){
System.out.println(e2.getMessage());
}
}
}

运行类

package testWin;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import javax.swing.JScrollPane;
import javax.swing.JTextArea; public class RunDialog extends JDialog { private final JPanel contentPanel = new JPanel();
JTextArea textArea = new JTextArea(14,20);
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
RunDialog dialog = new RunDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the dialog.
*/
public RunDialog() {
setBounds(100, 100, 686, 448);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane);
{
scrollPane.setViewportView(textArea);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
public void run(String name){
try{
Runtime ce1=Runtime.getRuntime();
Process proccess=ce1.exec("java "+name);
InputStream in1=proccess.getInputStream();
BufferedInputStream in2=new BufferedInputStream(in1);
byte a[]=new byte[100];
int n;
for(;(n=in2.read(a,0,100))!=-1;){
String s=new String(a,0,n);
textArea.append(s);
if(s==null)
System.out.println("333");
}
System.out.println(n);
System.out.println("444");
}
catch(Exception e){
System.out.println(e.getMessage());
}
} }


4,和第一个一样,读取数据库来答题,

import java.io.*;
import java.util.Scanner;
import java.sql.*; public class Test {
public static void main(String args[]){
ReadTest test1=new ReadTest();
test1.setDatabase("Kooing");
test1.setTable("test");
int number=test1.getNumber();
System.out.println("There are "+number+" question what are you want to answer");
Scanner scanner1=new Scanner(System.in);
for(;scanner1.hasNextInt();){
int num=scanner1.nextInt();
String huiche=scanner1.nextLine();
test1.setQusetion(num);
String s=test1.getQusetion();
System.out.println(s);
System.out.println("input your answer :");
String s2=scanner1.nextLine();
// String huiche=scanner1.nextLine();
if(s2.equals(test1.getAnswer().trim())){
System.out.println("right");
}
else{
System.out.println("error");
}
System.out.println("There are "+number+" question what are you want to answer");
}
}
} class ReadTest{
private Connection con;
private Statement sql;
private ResultSet rs;
private String database,table,daan,wenti;
private int question;
ReadTest(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(Exception e){
System.out.println(e.getMessage());
System.out.println("1111");
}
}
void setDatabase(String a){
database=a;
try{
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName="+database,"sa","kenn5666225");
}
catch(Exception e2){
System.out.println(e2.getMessage());
System.out.println("2222222222");
}
}
void setTable(String a){
table=a;
try{
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select *from "+table);
}
catch(Exception e3){
System.out.println(e3.getMessage());
System.out.println("33333333");
}
}
int getNumber(){
int n=0;
try{
rs.last();
n=rs.getRow();
}
catch(Exception e4){
System.out.println(e4.getMessage());
System.out.println("444444444");
}
return n;
}
void setQusetion(int a){
question=a;
}
String getQusetion(){
try{
rs.absolute(question);
wenti=rs.getString(1)+"\n"+rs.getString(2)+"\n"+rs.getString(3)+"\n"+rs.getString(4)+"\n"+rs.getString(5);
}
catch(Exception e5){
System.out.println(e5.getMessage());
System.out.println("55555555");
}
return wenti;
}
String getAnswer(){
try{
daan=rs.getString(6);
}
catch(Exception e6){
System.out.println(e6.getMessage());
System.out.println("6666666666");
}
return daan;
}
}

IO流+数据库课后习题的更多相关文章

  1. java基础IO流综合加习题

    IO流初学者在学习时都有一点迷糊,今天我们就讲讲IO流,希望通过讲解可以帮助大家 IO流分为字节流,字符流,缓冲流.我们只要记住这三个就可以了. 1*字节流有:字节输入流(FileInputStrea ...

  2. IO流的练习 —— 创建用户注册、登陆案例

    把上次用集合做的时候的分析拿出来 需求: 用户登录注册案例 按照如下操作,可以让我们更符合面向对象思想: A:这个案例有哪些类 B:每个类中又有哪些东西 C:类与类之间的关系 分析: A:这个案例有哪 ...

  3. JavaEE基础(二十一)/IO流

    1.IO流(字符流FileReader) 1.字符流是什么 字符流是可以直接读写字符的IO流 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出. 2 ...

  4. java中的IO流之文件复制

    O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...

  5. 【Android】数据存储-java IO流文件存储

    1.数据持久化:将在内存中的瞬时数据保存在存储设备中.瞬时数据:设备关机数据丢失.持久化技术提供一种机制可以让数据在瞬时状态和持久状态之间转换. 2.Android中简单的三种存储方式:文件存储.Sh ...

  6. IO流(一)

    一.异常 概述 异常就是Java程序在运行过程中出现的错误. 由来 问题也是现实生活中一个具体事务,也可以通过java的类的形式进行描述,并封装成对象.其实就是Java对不正常情况进行描述后的对象体现 ...

  7. Java IO 流总结篇

    1. 写在前面的话 I/O ,I 是 Input (输入)的缩写,O是Output (输出) 的缩写,众所周知,人与人之间想要沟通交流,就需要讲彼此都能听懂的语言,比如大家都统一说英语. 人类如果想和 ...

  8. JavaSE学习总结(十七)—— IO流

    一.IO流概要 1.1.概念 开发中经常要进行输入输出操作,掌握Java中的IO流显得非常必要. 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流, ...

  9. Java IO流学习

    Java IO流学习 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是 ...

随机推荐

  1. GitHub 入门

    1. CentOS 安装 Github. # sudo yum install skynet 安装之后查看一下版本. # git --version 2. 注册 Github 账号,登录后阅读 Git ...

  2. Struts2 DMI的使用

    Struts2的Action类中可以放置多个方法并在struts.xml中配置供页面调用.只要方法符合execute()方法的标准即返回类型是String就可以了. 同一个Action多个方法调用的方 ...

  3. 创建局域网内远程git仓库,并将本地仓库push推到远程仓库中

    转载请注明出处 http://www.goteny.com/articles/2014/06/136.html http://www.cnblogs.com/zjjne/p/3778640.html ...

  4. 【Oracle】ORACLE SQL Developer不支持JAVA版本

    ORACLE SQL Developer不支持JAVA版本 今天我打开 ORACLE SQL Developer准备开始练手.没有想到却给出了错误提示. 我 是安装了java JDK的而且是1.6版本 ...

  5. <<开源硬件创客 15个酷应用玩转树莓派>>

    本书共分18章,前3章是本书的基础章节,主要介绍了树莓派的一些基本情况和基本操作,来让读者了解树莓派的前世今生,掌握树莓派基本的使用方法.第4~18章主要介绍15个以树莓派为载体的酷炫应用,大家可以按 ...

  6. 2015WF有感

    World Final题目连接:http://icpc.baylor.edu/worldfinals/problems/icpc2015.pdf 建议:可以倒序阅读来获得最直观的赛场体验... 2:1 ...

  7. 【二分】【高精度】Vijos P1472 教主的集合序列

    题目链接: https://vijos.org/p/1472 题目大意: S1={1,2,3…n}.当i>1时,Si为集合Si-1中任意两个不相同数之和的集合. 将每个集合中所有元素取出,集合S ...

  8. Reverse Linked List II——LeetCode

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  9. 动态规划初级练习(二):BadNeighbors

    Problem Statement      The old song declares "Go ahead and hate your neighbor", and the re ...

  10. R学习日记——分解时间序列(非季节性数据)

    分解时间序列,就是将一个时间序列拆分成不同的构成元件.一般序列(非季节性序列)包含一个趋势部分和一个不规则部分(也就是随机部分),而如果是一个季节性序列,除以上两个外,还有季节性部分.   在此,我们 ...