繁体   English   中英

从 7 个数字的输入中找到最小的负双精度(十进制 - 双精度) - Java

[英]Find smallest negative double from Input of 7 numbers(decimal - double) - Java

我被要求写一个作业,我必须要求用户输入一个至少为 7 的数字,该数字确定他们必须输入的双精度数,对于第一个输入,我必须使用 while 循环,然后检查我必须使用 for 循环的双十进制数,以便我可以要求用户输入这 7 个双精度数。 最后,我必须显示总共输入了多少个数字,哪些数字是最小的负奇数。 先感谢您!

现在我的代码如下所示:


public class D6_4 {
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Type a number that is at least 7");
        int number = sc.nextInt();
        int count = 0;
        int condition = 0;

        while(number<7){
            System.out.println("Type a number that fulfills the condition at least 7");
            number = sc.nextInt();
        }

        sc.nextLine();

        double decimalNumber = 0;
        for(int i =0; i<number; i++){
            System.out.println("Type some decimal numbers");
            decimalNumber = sc.nextDouble();
            count++;
          
             /*here i need to create the condition for the code to check 
                which of the numbers is the smallest odd negative number*/
        }
       


    }

}

如果我说对了:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Type a number that is at least 7");
    int countDoubles = sc.nextInt();
    while (countDoubles < 7) {
        System.out.println("Type a number that fulfills the condition at least 7");
        countDoubles = sc.nextInt();
    }
    double smallestOddNegative = 0;
    for (int i = 0; i < countDoubles; i++) {
        System.out.println("Type some decimal number");
        double currentDouble = sc.nextDouble();
        if (currentDouble % 2 == -1 && currentDouble < smallestOddNegative) {
            smallestOddNegative = currentDouble;
        }
    }
    System.out.printf("%s %f\n", "Smallest odd negative", smallestOddNegative);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM