Archive for 五月, 2014

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

import java.io.*;
public class Ex12Class {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileIn = "score.txt";
		String fileOut = "sum.txt";
		try
		{
			//In
			FileReader reader = new FileReader(fileIn);
			BufferedReader sr = new BufferedReader();
			//Out
			FileWriter writer = new FileWriter(fileOut);
			PrintWriter pw = new PrintWriter();
 
			String line = sr.readLine();
			while (line != null && line.length() > 0)
			{
				String[] scores = line.split(" ");
				int s1 = Integer.parseInt(scores[0].trim())
				int s2 = Integer.parseInt(scores[1].trim())
				int s3 = Integer.parseInt(scores[2].trim())
				double avg = 1.0 *(s1 + s2 + s3)/3.0; 
				System.out.printf("%2d %2d %2d %7.3f\n", s1, s2, s3, avg);
				        pw.printf("%2d %2d %2d %7.3f", s1, s2, s3, avg);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: , , ,

public class Ex11Class {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String idStr =  args[0];
		int num = Integer.parseInt(idStr);
		int len = idStr.length();
		int odds = 0;
		for (int i=0; i< len; i++)
		{
			int remainder = num % 2;
			if (remainder == 1)
				odds++;
			//num = num /10;
			num /= 10;
		}
		System.out.printf("There are %d odds and %d evens in My ID %s", odds, len - odds, idStr);
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,