Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication348;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.Random;
- public class JavaApplication348 {
- public static void main(String[] args) {
- Random e = new Random();
- Runtime r = Runtime.getRuntime();
- int ilosc = 0;
- int ilosc2 = 0;
- int liczba = 0;
- int suma = 0;
- double srednia = 0;
- try {
- RandomAccessFile f = new RandomAccessFile("D:/dane.dat", "rw");
- for (int i=0;i<20;i++){
- liczba = e.nextInt(100);
- f.writeInt(liczba);
- }
- f.seek(0);
- while(f.getFilePointer()<f.length()){
- liczba=f.readInt();
- if(liczba%3==0){
- suma+=liczba;
- System.out.print(liczba+", ");
- ilosc++;
- }
- }
- System.out.println();
- System.out.println("Liczba podzielnych przez 3: "+ilosc);
- System.out.println();
- f.seek(0);
- while(f.getFilePointer()<f.length()){
- liczba=f.readInt();
- if(liczba%2==0){
- suma+=liczba;
- System.out.print(liczba+", ");
- ilosc2++;
- }
- }
- System.out.println();
- System.out.println("Liczba podzielnych przez 3: "+ilosc2);
- f.close();
- } catch (IOException ex) {
- System.out.println("Błąd pliku");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement