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

小白求教各位大神,关于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左右吧

    打赏 评论

相关推荐 更多相似问题