Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package javaapplication1;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.*;
  12.  
  13. /**
  14.  *
  15.  * @author DOM
  16.  */
  17. public class Main {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main (String[] args)throws IOException {
  23.         // TODO code application logic here
  24.  
  25.         System.out.println("Tablica:");
  26.         int [] tablica=new int[20];
  27.         Random generator=new Random();
  28.         for (int i=0; i<tablica.length; i++){
  29.             tablica[i]=generator.nextInt(10);
  30.             System.out.print("[" +(tablica[i])+ "]");
  31.         }
  32.  
  33.         System.out.println("\nPodaj liczbę całkowitą z przedziału od 1 do 9:");
  34.         int a = 0;
  35.         BufferedReader odczyt = new BufferedReader(new InputStreamReader(System.in));
  36.         String temp = odczyt.readLine();
  37.                         try {
  38.                                 a = Integer.parseInt(temp);
  39.                                 if (a>9) System.out.println("Podana liczba jest za duża (max. 9)");
  40.                                 if (a<1) System.out.println("Podana liczba jest za mała (min. 1)");
  41.                                 System.exit(1);
  42.                             }
  43.                         catch (NumberFormatException ex) {
  44.                                 System.out.println("Podany ciąg znaków nie jest liczbą!");
  45.                                 System.exit(1);
  46.                             }
  47.        
  48.         int liczba=0;
  49.         for (int i=0; i<tablica.length; i++){
  50.             if (tablica[i]==a){
  51.                 liczba++;
  52.             }
  53.         }
  54.         System.out.println("["+liczba+"]");
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement