Advertisement
farezowskyy

odczyt zapis losowanie xD

Jan 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1.  
  2. package javaapplication348;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.Random;
  6.  
  7. public class JavaApplication348 {
  8.  
  9.  
  10. public static void main(String[] args) {
  11. Random e = new Random();
  12. Runtime r = Runtime.getRuntime();
  13. int ilosc = 0;
  14. int ilosc2 = 0;
  15. int liczba = 0;
  16. int suma = 0;
  17. double srednia = 0;
  18. try {
  19. RandomAccessFile f = new RandomAccessFile("D:/dane.dat", "rw");
  20. for (int i=0;i<20;i++){
  21. liczba = e.nextInt(100);
  22. f.writeInt(liczba);
  23. }
  24. f.seek(0);
  25. while(f.getFilePointer()<f.length()){
  26. liczba=f.readInt();
  27. if(liczba%3==0){
  28. suma+=liczba;
  29. System.out.print(liczba+", ");
  30. ilosc++;
  31.  
  32. }
  33.  
  34. }
  35. System.out.println();
  36. System.out.println("Liczba podzielnych przez 3: "+ilosc);
  37. System.out.println();
  38.  
  39.  
  40.  
  41. f.seek(0);
  42. while(f.getFilePointer()<f.length()){
  43. liczba=f.readInt();
  44. if(liczba%2==0){
  45. suma+=liczba;
  46. System.out.print(liczba+", ");
  47. ilosc2++;
  48. }
  49. }
  50. System.out.println();
  51. System.out.println("Liczba podzielnych przez 3: "+ilosc2);
  52. f.close();
  53. } catch (IOException ex) {
  54. System.out.println("Błąd pliku");
  55. }
  56.  
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement