java-我得到错误的计数结果

时间:2013-11-20 07:02:44

标签: java

我写了一个程序,计算每个班级的房间数量(艺术,体育等)。

例如,每个班级最多包含3名学生。如果我有9名学生,有6名音乐学生和3名艺术学生,结果必须是:1个艺术空间和2个音乐室。

当我运行代码时,我得到了错误的结果:艺术的房间数等于2而不是1,音乐的房间数等于1而不是2.

以下是执行此程序的结果:

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

这是我的代码:

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<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);
            }      
        }
    }
}

4 个答案:

答案 0 :(得分:0)

问题在于countOFR。您使用它来存储所有类的房间,而不是任何类。因此,当您读取类的数据时,会添加较早的countOFR值。

            if(M==3)
            {
            countOFR++;   // This should not be common for all
            int countOfRoom=+countOFR;
            System.out.println("The number of rooms for Music is:"+countOfRoom);
            }

改为使用不同的变量

int countOFR_music = 0;
int countOFR_art = 0;
int countOFR_theatre = 0;
int countOFR_sport = 0;

然后像这样使用它

            if(M==3)
            {
            countOFR_music++;   // This should not be common for all
            int countOfRoom=+countOFR_music;
            System.out.println("The number of rooms for Music is:"+countOfRoom);
            }

同样适用于艺术课

                if(A==3)
                {
                countOFR_art++;
                int countOfRoom=+countOFR_art;
                System.out.println("The number of rooms for Art is:"+countOfRoom);
                }

答案 1 :(得分:0)

这是一个更短的代码:

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[] counts = new int[4]; //This variable contains the number of the participants in music, arts, theatre and sports classes in its 4 cells.

      for(int i=0;i<participantNo;i++)
      {
             System.out.println("Insert student in class:");
             int p= GActivity.nextInt();// // Representation of student by number.int 
             if(p<=4)
                 counts[p-1]++;
      }

      System.out.println("The number of rooms for Music is:" + Math.ceil(counts[music-1]/3.0));
      System.out.println("The number of rooms for Art is:" + Math.ceil(counts[art-1]/3.0));
      System.out.println("The number of rooms for Theatre is:" + Math.ceil(counts[theatre-1]/3.0));
      System.out.println("The number of rooms for Sports is:" + Math.ceil(counts[sport-1]/3.0));

    }
}

答案 2 :(得分:0)

我认为你应该计算每个班级的学生,然后在最后处理它们。

首先,声明一个或多个对象以容纳学生人数。您的声明可以正常工作,但为了缩短您的代码,您应该使用数组:

int numOfStudent[] = new int[4]; // 4 is for 4 types of class

然后在for循环中,在增加计数器之前进行检查,可能会再次询问使用输入(这取决于你):

// ...
int p= GActivity.nextInt();
int idx = p - 1; // Convert to zero-base index
if (idx >= 0 && idx < numOfStudent.length()) {
    numOfStudent[idx]++;
}

最后,出于for循环,只需处理它们。因为每个班级最多包含3名学生,我们有:

int numOfMusicClass = (int)Math.ceil(numOfStudent[music - 1] / 3.0);
// ... Do the same for other class

希望这个解决方案可以帮到你!

此外,您的班级类型由数字定义,因此,您应该打印出代表的数字。例如:

  

在课堂上插入学生(1 - 音乐,2 - 艺术,3 - 剧院,4 - 运动):

它会让用户知道他/她输入的内容。

答案 3 :(得分:-2)

编写较小的功能。这太大了,无法阅读。