Note that there are some explanatory texts on larger screens.

plurals
  1. POjava- I got wrong results of counting
    text
    copied!<p>I wrote a program which counts the number of rooms for each class (art, sport, etc).</p> <p>For example, each class contains a maximum of 3 students. If I have 9 students, with 6 music students and 3 art students,the results have to be: 1 room for art and 2 rooms for music.</p> <p>When I run the code, I get these wrong results: the number of rooms for art equals 2 instead of 1 and the number of rooms for music equals 1 instead of 2. </p> <p><strong>Here is the result of execution of this program:</strong></p> <pre><code>Insert numbers of participants: 9 Insert student in class: 1 Insert student in class: 1 Insert student in class: 1 The number of rooms for Music is:1 Insert student in class: 1 Insert student in class: 1 Insert student in class: 1 Insert student in class: 2 Insert student in class: 2 Insert student in class: 2 The number of rooms for Art is:2 </code></pre> <p><strong>This is my code:</strong></p> <pre><code>import java.util.Scanner; public class GroupActivity { public static void main() { 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. int countOFR=0; for(int i=0;i&lt;participantNo;i++) { System.out.println("Insert student in class:"); int p= GActivity.nextInt();// // Representation of student by number.int if(p==music) //for( { countM++; int M=countM; //System.out.println("student in class:"+music); //System.out.println("Total music class:"+M); if(M==3) { countOFR++; int countOfRoom=+countOFR; System.out.println("The number of rooms for Music is:"+countOfRoom); } } else if(p==art) { countA++; int A=countA; // System.out.println("student in class:"+art); if(A==3) { countOFR++; int countOfRoom=+countOFR; System.out.println("The number of rooms for Art is:"+countOfRoom); } //System.out.println("Total student in art class:"+A); } else if(p==theatre) { countT++; int T=countT; // System.out.println("student in class:"+theatre); //System.out.println("Total thaetre class:"+T); if(T==3) { countOFR++; int countOfRoom=+countOFR; System.out.println("The number of rooms for Theatre is:"+countOfRoom); } } else{ countS++; int S=countS; if(S==3) { countOFR++; int countOfRoom=+countOFR; System.out.println("The number of rooms for Sport is:"+countOfRoom); } //System.out.println("Total sport class:"+S); } } } } </code></pre>
 

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