【问题标题】:I don't know why I cannot get the output I expected我不知道为什么我不能得到我期望的输出
【发布时间】:2022-01-24 06:59:53
【问题描述】:

这是我想得到的输入..但我没有得到它。另外,我也不确定我的编码是否正确,因为这是我第一次。

输入员工编号:785
第一个和最后一个数字的总和 = 12
分配给 A 队
输入员工编号:53
第一个和最后一个数字的总和 = 8
分配给 A 队
输入员工编号:9128
第一个和最后一个数字的总和 = 17
分配给 B 组
输入员工编号:39998
第一个和最后一个数字的总和 = 11
分配给 B 组
输入员工编号:123456
团队 A 成员数 = 2
B 组成员数 = 2

然而,我最后得到了这个:

团队 A 成员的数量 = 0
B 组成员数 = 0

我应该如何正确编写计算团队成员数的方法?

package STIA1014Asg2.zip;  
import java.util.*;  


public class nn {
    public static int getDigits(int num) { return String.valueOf(num).length(); }
 
    public static int getLast(int num) { return num % 10; }
  
    public static int getFirst(int num) {
        int first = 0;
        while(num != 0) {
           first = num % 10;
           num /= 10; 
        }
        return first;
    }
    private static class Group {
        public List<Integer> teamA = new ArrayList<>();
        public List<Integer> teamB = new ArrayList<>();

        public Group() {
        }
    
        private void display() {
             System.out.println("Count of Team A members = " + teamA.size());
             System.out.println("Count of Team B members = " + teamB.size());
            }

        private void add(int id, boolean b) {
            
        }
    }

    public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      Group group = new Group(); 
      
      
      while(true) {
          System.out.print("Enter the employee ID: ");
          int id = scan.nextInt(); 

          int digits = getDigits(id); 
          if(digits > 5 || digits < 2) {  
              group.display();  
              break; 
          }

          int first = getFirst(id); 
          int last = getLast(id);   
          int sum = first + last;   
          System.out.println("Sum of first & last digit = " + sum); 
          
          if(sum % 2 == 0) {  
              group.add(id, true);
              System.out.println("Assigned to Team A");
          }
          else { 
              group.add(id, false);
              System.out.println("Assigned to Team B");
          }
      }
    }
}

【问题讨论】:

    标签: java sum


    【解决方案1】:

    您的 add 方法可能看起来像这样:

       private void add(int id, boolean b) {
    
            if(b){
                teamA.add(id);
            }
            else{
                teamB.add(id);
            }
            
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 2022-10-23
      • 2018-10-11
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多