0

This is a simple program to count the numbers of positive ,negatives and zeros

   int zeros=0,positive=0,negitive=0;
        char c;
    do {
        System.out.println("enter a number");
        int x=sc.nextInt();

        if (x==0) {
            zeros++;
        }
        else if(x>0){
            positive++;
        }
        else
        negitive++;
        
        System.out.println("do you want to enter more?(y/n)");
        c=sc.next().charAt(0);
        
       } while (c =='y');
       System.out.println("zeros "+zeros);
       System.out.println("positive "+positive);
       System.out.println("negitive "+negitive);

but when i take input as String with charAt(0) while loop doesnt work why so ?

 int zeros=0,positive=0,negitive=0;
        String c;
    do {
        System.out.println("enter a number");
        int x=sc.nextInt();

        if (x==0) {
            zeros++;
        }
        else if(x>0){
            positive++;
        }
        else
        negitive++;
        
        System.out.println("do you want to enter more?(y/n)");
        c=sc.next().charAt(0);
        
       } while (c =="y");
       System.out.println("zeros "+zeros);
       System.out.println("positive "+positive);
       System.out.println("negitive "+negitive);

i was trying to take a char input with string data type with charAt(0) method but the while loop was not working.confused with these two

Lakshit
  • 1
  • 1

0 Answers0