Posts Tagged ‘Java’

4
五月

程式設計二程式 05/04: 期中考

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

import java.io.*;
//Mid1a
		int a, b, c, all;
		a = Integer.parseInt(args[0]);
		all = a * 5 + b * 2 + c;
		System.out.printf("input=(%d, %d, %d) ans = %d", a, b, c, all);
//Mid1b
import java.io.*;
public class Mid1b {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			FileWriter writer = new FileWriter("package.txt");
			PrintWriter pw = new PrintWriter(writer);
            int largeCount = 1000/5;
			for (int i=largeCount;  i>=0;  i--)
			{
				int j = (1000 - 5 * i)/2;
				System.out.printf("Large= %3d Small = %3d\n" , i, j);
				pw.printf("Large= %3d Small = %3d" , i, j);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: , , ,

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: , , ,

13
四月

程式設計二程式 04/13: ex5

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

import java.io.*;
public class Ex5Class {
	public static void main(String[] args) {
		//建立亂數的物件
		Random rnd = new Random();
		try
		{
			FileWriter writer = new FileWriter("arrival.txt");
			PrintWriter pw = new PrintWriter(writer);
			//產生1000個亂數
			for (int i=0  i<1000  i++)
			{
				int time = rnd.nextInt();////產生1個亂數
				if (time  0) time=-time;
				time = time %30 + 1 
				System.out.printf( %2d\n , time) 
				pw.printf("%2d", time);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: , , ,

30
三月

程式設計二程式 03/30: ex4

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

import 
import java.io.*;
public class Ex4Class {
	public static void main(String[] args) {
		String fileIn = "lotto txt";
		int count = 0;
		int countSix = 0; 
		int[] nums = new int[20]; 
		try
		{
			FileReader reader = new FileReader();
			BufferedReader sr = new BufferedReader();
 
			String line = sr.readLine();
			while (line !=null && line.length()>0)//for (int i=0; i<10; i++)
			{
				String[] numStr = line.split(" ");
				int sixes = 0;
				for (int i=0; i< numStr.length; i++)
				{
					if (numStr[i].trim().length()==0)
						continue;
					nums[i] = integer.ParseInt(numStr[i].trim());
					if (nums[i]==6|nums[i]==16|nums[i]==26||nums[i]==36||nums[i]==46)
						sixes++;
				}
				if (sixes >0)
				{
					countSix++;
					System.out.println(line); 
				}
				count++;
			    line = sr.readLine();
			}
			sr.close();
		}
		//catch (FileNotFoundException fe)
		{
			System.out.printf(fe.getMessage());
		}
		//catch (IOException fe)
		{
			System.out.printf(fe.getMessage());
		}
		System.out.printf("Total lines = %d Six Lines = %d (%f%%), count, countSix, (double)(countSix)*100/count);
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,

23
三月

程式設計二程式 03/23: ex3

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

import java. io. *;
public class Ex3Class {
 
	public static void main(String[] args) {
		long id;
		id = Long.parseLong(args[]);
		int even = 0;
		int odd = 0;
		//id = 1032245432
		for (int i=0; i< 9; i++)
		{
			if (id %2 ==0)
				even;//even = even + 1;
			else
				odd++;
			id =10;//id = id /10;
		}
		// TODO Auto-generated method stub
		System.out.printf("Even= %d Odd = %d", even, odd);
	}
 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,