Posts Tagged ‘資工1’

import java.io.*;
public class FinalClass {
 
	public static void main(String[] args) {
		try
		{
			//In
			FileReader reader = new FileReader("math.txt");
			BufferedReader sr = new BufferedReader(reader);
			//Out
			FileWriter writer = new FileWriter(result.txt);
			PrintWriter pw = new PrintWriter(writer);
 
			String line = sr.readLine();
			double sum = 0.0, avg=0.0;
			int count = 0;
			while (line != null && line.length() > 0)
			{
				int score = Integer.parseInt(line);
				double modified; 
				if (score >=60)
					modified = 20.0 + score;
				else
					modified = 0.5 *(score)+ 30; 
				sum += modified; count++;
				System.out.printf("%2d => %5.2f\n", score, modified);
				        pw.printf("%5.2f", modified);pw.println()
				line = sr.readLine();
			}
			sr.close();		
			pw.close();
			if (count > 0)//計算平均
				avg = sum / count;
			System.out.printf("Average = %5.2f", avg)
		}
		catch(FileNotFoundException fe)
		{
			System.out.printf(fe.getMessage());
		}
		catch(IOException ie)
		{
			System.out.printf(ie.getMessage());
		}
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,

10
六月

程式設計二程式 06/10: Quiz2.java

   Posted by: admin    in 102(下)程式設計(二)

import java.io.*;
public class Quiz2 {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
 
		String fileIn = record.txt;
		String fileOut = final.txt;
		try
		{
			//In
			FileReader reader = new FileReader();
			BufferedReader sr = new BufferedReader();
			//Out
			FileWriter writer = new FileWriter();
			PrintWriter pw = new PrintWriter();
 
			String line = sr.readLine();
			while (line != null && line.length() > 0)
			{
				//int score = Integer.parseInt(line);
				//double modified = 0.5 *(score)+50; 
 
				//System.out.printf("%2d => %5.2f\n", score, modified);
				//        pw.printf("%5.2f", modified);pw.println();
				//line = sr.readLine();
			}
			sr.close();		
			pw.close();
		}
		//catch(FileNotFoundException fe)
		{
			System.out.printf(fe.getMessage());
		}
		//catch(IOException ie)
		{
			System.out.printf(ie.getMessage());
		}
	}
 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,

import java.awt.event.*;
import javax.swing.*;
public class Ex14ActionListener implements ActionListener {
 
	public void actionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(null, "Hello");
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,

import java.awt.EventQueue;
import javax.swing.*;
public class Ex14Class{
	private JFrame frame;
	private JButton btn;
	private Ex14ActionListener listener;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Ex14Class window = new Ex14Class();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Ex14Class() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Java Swing");
		frame.setLayout(null);//不使用版面配置
		listener = new Ex14ActionListener();
		btn = new JButton("Click me!");
		btn.addActionListener(listener);
		btn.setBounds(180, 100, 100, 50);
		frame.add(btn);
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,

import java.io.*;
import java.util.*;
public class Ex13Class {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileOut = "score.txt";
		Random random = new Random();
		try
		{
			//Out
			FileWriter writer = new FileWriter(fileOut);
			PrintWriter pw = new PrintWriter(writer);
			for (int i=0; i< 100; i++)
			{
				int a = random.nextInt();
				if (a < 0)
					a = -a;
				int b = a % 90 + 10;
				System.out.printf("%2d\n",b);
				        pw.printf("%2d",b);pw.println();
			}
			pw.close();
		}
		catch(FileNotFoundException fe)
		{
			System.out.printf(fe.getMessage());
		}
		catch(IOException ie)
		{
			System.out.printf(ie.getMessage());
		}
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,