20
四月

程式設計二程式 04/20: Quiz1

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

import java.io.*;
//Quiz1a
		int a, b, c, all;
		a = Integer.parseInt(args[0]);
		all = a * 4 + b * 2 + c;
		System.out.printf("input=(%d, %d, %d) ans = %d", a, b, c, all);
//Quiz1b
		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 =  0.8 * score + 20;//計算式一
				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: , , ,

This entry was posted on 星期一, 四月 20th, 2015 at 10:50:44 and is filed under 103(下)程式設計(二). You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment