Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Element;
- import java.util.Scanner;
- import java.util.Stack;
- /**
- *
- * @author MaxSylverWolf
- */
- public class Nawiasy {
- public static void main(String[] args) {
- Stack stos = new Stack();
- String ciagZnakow;
- System.out.println("Podaj nawiasy, program sprawdza, czy są one prawidłowe (domyślnie, czy się zamykają)");
- Scanner skanuj= new Scanner(System.in);
- ciagZnakow= skanuj.nextLine();
- char[] Lista= ciagZnakow.toCharArray();
- char nawias1 = '(';
- char nawias2 = ')';
- int ciag =ciagZnakow.length();
- int k=0,l=0;
- for (int i = 0; i < ciagZnakow.length(); i++) {
- stos.push(ciagZnakow.charAt(i));
- }
- for (int i=0; i<ciag;i++)
- {
- if (Lista[i] == nawias1) k++;
- if (Lista[i] == nawias2) l++;
- stos.pop();
- }
- if (k==1 && l==0)System.out.println("Złe nawiasy!!");
- if (k==0 && l==1)System.out.println("Złe nawiasy!!");
- if (k==l)System.out.println("Dobre nawiasy");
- else if (k!=l)
- System.out.println("Złe nawiasy!!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement