Elsa镇魂女孩 2019-02-21 02:25 采纳率: 62.5%
浏览 452
已采纳

小白求教各位大神,关于java,求余数

分别输入两个数double a,b;
    r = b - count * a ;  
    求count为几的时候,r=1.0;

    例如,double a = 1.3;
         double b = 3.9;
     那么,count = 3

import java.util.Scanner;

public class Exg {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.println("Pleat enter the first number!");
        double a = s.nextDouble();

        Scanner w = new Scanner(System.in);
        System.out.println("Pleat enter the second number (larger than the first number) !");
        double b = w.nextDouble();
        double count=1.0;
        double r;
        do { 
            r = b- count * a;
            count ++;
        }while(r==1.0);

        System.out.println(count);


    }   
}

结果大概的样子为:

Pleat enter the first number!
2.3
Pleat enter the second number (larger than the first number) !
36.9
2.0


好忧伤啊, 不论添什么数,结果都是2.0

  • 写回答

2条回答 默认 最新

  • Elsa镇魂女孩 2019-02-21 04:53
    关注

    import java.util.Scanner;

    public class Exg {

    public static void main(String[] args) {
    
        Scanner s = new Scanner(System.in);
        System.out.println("Pleat enter the first number!");
        double a = s.nextDouble();
    
        Scanner w = new Scanner(System.in);
        System.out.println("Pleat enter the second number (larger than the first number) !");
        double b = w.nextDouble();
    
        double d = (b-1.0)/a;
        int count = (int) d;
        System.out.println(count);
    
    
    }   
    

    }

    结果:
    Pleat enter the first number!

    2.1
    Pleat enter the second number (larger than the first number) !
    10.1
    4

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • Dankin_ 2019-02-21 08:05
    关注

    do {
    r = b- count * a;
    count ++;
    }while(r==1.0);

    楼主你的这一步有问题
    首先count并不是整数,所以你使用count++是不考虑count++和count之间的数字的
    你这个函数会先计算count==1时对应的r,然后count由1.0增加为2.0,此时r!=1.0,while循环终止,count就停止在2了
    正确的应该是直接使用计算式子 count = (b-r)/a直接输出
    顺便提一句,你举的例子也不对,b=3.9,a=1.3时,count应该等于(3.9-1.0)/1.3即2.9/1.3,大概在2.24左右吧

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java连接sqlserver有问题
  • ¥15 yolov8 如何调cfg参数
  • ¥15 这个四人抢答器代码哪儿有问题?仿真程序怎么写?
  • ¥15 burpsuite密码爆破
  • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
  • ¥15 scikit安装之后import不了
  • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
  • ¥15 人脸识别相关算法 YOLO,AI等
  • ¥15 spark问题方便加V17675797079
  • ¥15 Python代码不打印的原因