Note that there are some explanatory texts on larger screens.

plurals
  1. POwrong result of counting
    primarykey
    data
    text
    <p>I'm writing a program which counts the total number of students in each class (music, art, theatre, sport). If i had 3 students, and two of them take the art class and one takes music class, the results should be: art -2 and music -1. I got a wrong result.</p> <p>comment: the music class represent by number one and art by 2.</p> <p>Results:</p> <p>Insert numbers of participants: 3 Insert student in class: 2 student in class:2 Total:1 student in class:4 Total:0 Insert student in class: 2 student in class:2 Total:3 student in class:4 Total:1 Insert student in class: 1 student in class:1 Total:1 student in class:4 Total:2</p> <p>My code:</p> <p>import java.util.Scanner; public class GroupActivity { public static void main()</p> <pre><code>{ Scanner GActivity=new Scanner(System.in); System.out.println("Insert numbers of participants:"); int participantNo= GActivity.nextInt();//Insert numbers of participants int music= 1;int art=2; int theatre= 3; int sport= 4; // Representation of each class by numbers. int countM=0; //This variable contains the number of the participants in music class. int countA=0; //This variable contains the number of the participants in art class. int countT=0; //This variable contains the number of the participants in theatre class. int countS=0; //This variable contains the number of the participants in sport class. for(int i=0;i&lt;participantNo;i++) { System.out.println("Insert student in class:"); int p= GActivity.nextInt();// // Representation of student by number. if(p==music) { countM++; System.out.println("student in class:"+music); System.out.println("Total:"+countM++); } else if(p==art) { countA++; System.out.println("student in class:"+art); System.out.println("Total:"+countA++); } else if(p==theatre) { countT++; System.out.println("student in class:"+theatre); System.out.println("Total:"+countT++); } else countS++; System.out.println("student in class:"+sport); System.out.println("Total:"+countS++); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload