【问题标题】:Incorrect output with counting计数输出不正确
【发布时间】:2014-11-18 12:34:06
【问题描述】:

我正在编写一个程序,该程序将继续接受数字,直到用户输入 -1。然后它将显示可被 9 整除的项目数;使用一个特殊的属性——能被 9 整除的数字的数字之和,它们本身也能被 9 整除。有人告诉我要依赖 高度模块化 方法。简单的输入,没有数组。

没有编译错误之类的东西,但是count的值总是错误的。比如我输入18,18,4,4,2,5,19,36,-1,预期的输出是3,但是输出出来了如 4. 我不知道为什么。我试过count++和++count,它们产生相同的输出。我哪里错了?

import java.util.Scanner;
public class Div{
static Scanner sc = new Scanner(System.in); boolean run = true; static int n = 0; 
static int count = 0;
public static void main(String[] args){

    Div a = new Div();
    a.accept();
    a.display();

}
void accept(){
    while (run){
        System.out.println("Enter the number");
        n = sc.nextInt();
        if (isDivisibleByNine(n)){
            count = count + 1;
        }
        if (n == -1){
            run = false;
        }
    }
 }
 static int sumOfDigits(int a){
    int m = a; int sum = 0;
    while (m>0){
        sum = sum + (m%10); m /= 10;
    }
    return sum;
 }
static boolean isDivisibleByNine(int x){
    if (sumOfDigits(x)%9==0){
        return true;
    }
    else {
        return false;
    }

}
void display(){
    System.out.println("The total number of numbers that are divisible are: " + count);
}
}

【问题讨论】:

  • 你试过输出那些被认为是可分的吗?然后您可以查看是哪一个导致了问题并从那里进行调试。
  • 先尝试if(n==-1),然后再尝试else if (isDivisibleByNine(n))
  • @CoolGuy,很棒的答案!编码差异如此之小,输出差异如此之大。极好!现在工作正常:)

标签: java input output


【解决方案1】:

问题在于您的最终值 -1。对于 -1,您的 sumOfDigits 函数也将返回 0。因此只有 count 始终增加 1。

while (run){
    System.out.println("Enter the number");
    n = sc.nextInt();
    if (n == -1){
        break; //Break if the end of input reached.
    }
    if (isDivisibleByNine(n)){
        count = count + 1;
    }

}

【讨论】:

    【解决方案2】:

    您的程序认为可以被 9 整除的最终数字是您的 -1。

    您为它完成了完整的循环-您的总和仅在数字大于 0 时才进行,因此它立即终止并返回 0。0 % 9 为 0,因此它会增加您的计数器然后结束。通过更改循环来检查这一点:

        if (n == -1){
            run = false;
        }
       else if (isDivisibleByNine(n)){
            count = count + 1;
        }
    

    【讨论】:

      【解决方案3】:

      您没有计算数字的总和。

      这是你计算它的方式:

       static int sumOfDigits(int a){
          int m = a; int sum = 0;
          while (m>0){
              sum = sum + (m%10);
              m /= 10;
          }
          return sum;
       }
      

      编辑(在您修复 sumOfDigits 之后):

      您得到了错误的计数,因为您还检查了 -1 是否可以被 9 整除,并且它返回 true,因为 sumOfDigits 不能正确处理负整数。

      这是一个简单的解决方法:

      void accept(){
          while (run){
              System.out.println("Enter the number");
              n = sc.nextInt();
              if (n == -1){
                  run = false;
              }
              else if (isDivisibleByNine(n)){
                  count = count + 1;
              }
          }
       }
      

      【讨论】:

      • 我修复了它,但即使这样,也没有运气。
      【解决方案4】:

      改成这样。

      static boolean isDivisibleByNine(int x){
      if(x>0){
      if (sumOfDigits(x)%9==0){
          return true;
      }
      else {
          return false;
      }
      }else{
          return false;
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 2020-03-29
        • 2018-09-06
        • 1970-01-01
        • 2021-03-05
        • 2015-01-08
        • 1970-01-01
        相关资源
        最近更新 更多