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. Android 判断当前网络连接类型

    实际应用开发时,如果存在需要用户获取大量数据的情况,最好是先判断下网络类型,提示用户当前的网络类型,是否需要连接Wifi,etc.(手机流量太贵啦,当然土豪是无视这玩意的, (/ □ \)). 定义网 ...

  2. CENTOS纯手工安装LAMP+PHPMYADMIN

    现在,安装这些确实越来越方便了. Installing Apache2 With PHP5 And MySQL Support On CentOS 6.4 (LAMP) 参考URL: http://w ...

  3. AJAX中的请求方式以及同步异步的区别

    AJAX中的请求方式以及同步异步的区别请求方式,分为GET与POST: GET 最为常见的HTTP请求,普通上网浏览页面就是GET.GET方式的参数请求直接跟在URL后,以问号开始.(JS中用wind ...

  4. 男装电商Bonobos融资5500万美元,计划IPO,全靠体验店战略 - 国外 - 快鲤鱼

    男装电商Bonobos融资5500万美元,计划IPO,全靠体验店战略 - 国外 - 快鲤鱼 男装电商Bonobos融资5500万美元,计划IPO,全靠体验店战略

  5. 无需转化直接使用ESD映像文件安装系统简明教程

    原版系统ISO镜像的sources文件夹中包含install.wim映像文件,将这个WIM文件“解压”(官方术语“Apply”)后,可以看到和C盘的目录完全相同,即为系统文件. 而官方提供的原版ESD ...

  6. Byte[]、Image、Bitmap_之间的相互转换

    1.将图片Image转换成Byte[] /// <summary>        /// 将图片Image转换成Byte[]        /// </summary>     ...

  7. Java并发编程:并发容器ConcurrentHashMap

    Java并发编程:并发容器之ConcurrentHashMap(转载) 下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concu ...

  8. maven项目 打可执行jar包

    1.pom添加 <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</art ...

  9. 2013级C++第15周(春)项目——输入输出流及文件文件操作

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本周程序阅读及程序调试中须要的文件,请到htt ...

  10. Android TabActivity之感叹

    (一)前言 在以前一篇帖子讲ams的时候,提了一下TabActivity.当时说它比较特殊就没有下文了,今天重发一篇帖子,跟大家探讨一下TabActivity. 做个假定先: 比如我们最外面的Acti ...