• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

example of count++

 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
can someone give me a simple example of how to read different sets of inputs..counts the matching inputs and outputs the number they have reoccured..
o.k..lets hypethitically say..i wana input 3 different types of cows; black, brown and white..and wana count how many times they have been inputed and output them by saying black cows 3 brown cows 4..etc
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Marilyn
i have done something similar but its not working,i'm try tocount and then catgorize grades instead of cows would i be better off with arrays u think.. can you see where i went wrong?
public class GradeCount
{


public static void main (String[] args)


int grade, count = 0, sum = 0;





System.out.println ("Enter the first Grade");
grade = Keyboard.readInt();

while( grade = 90 && < 100)

{
count++;
sum += grade;
System.out.println (" Number of A's = "+ count);

}

while( grade = 80 && < 89)

{
count++;
sum += grade;
System.out.println (" Number of B's = "+ count);

}

while( grade = 70 && < 79)

{
count++;
sum += grade;
System.out.println (" Number of C's = "+ count);

}


while( grade = 60 && < 69)

{
count++;
sum += grade;
System.out.println (" Number of D's = "+ count);

}

while( grade = 0 && < 59)

{
count++;
sum += grade;
System.out.println (" Number of F's = "+ count);

}
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, you could use arrays, but I think it is unnecessary. I think you might want to keep the count of each grade separately rather than one count for the total number of all grades combined.

I also think you might run into problems using while instead of if. I would think you would get a stack overflow error with these while loops.

By the way, code tags are a good thing.
 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i'm getting these types of error: illegal start of expression, incompatible types, '}' expected, and finally cannot resolve symbol class Keyboard
the code is below..can any one help me eliminate these errors

import cs1.Keyboard;
public class GradeCount
{


public static void main (String[] args)

{

int grade, count = 0, sum = 0;





System.out.println ("Enter the first Grade");
grade = Keyboard.readInt();

if( grade = 90 && < 100)


count++;
sum += grade;
System.out.println (" Number of A's = "+ count);



if( grade = 80 && < 89)


count++;
sum += grade;
System.out.println (" Number of B's = "+ count);



if( grade = 70 && < 79)


count++;
sum += grade;
System.out.println (" Number of C's = "+ count);




if( grade = 60 && < 69)


count++;
sum += grade;
System.out.println (" Number of D's = "+ count);



if( grade = 0 && < 59)


count++;
sum += grade;
System.out.println (" Number of F's = "+ count);

}

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic