【问题标题】:Find smallest negative double from Input of 7 numbers(decimal - double) - Java从 7 个数字的输入中找到最小的负双精度(十进制 - 双精度) - Java
【发布时间】:2021-08-22 21:36:26
【问题描述】:

我被要求写一个作业,我必须要求用户输入一个至少为 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*/
        }
       


    }

}

【问题讨论】:

    标签: java string methods contains extends


    【解决方案1】:

    如果我猜对了:

    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);
    }
    

    【讨论】:

    • 嗨@Ivan,谢谢你的回答。但是输出似乎不太正确,看看: 输入一个至少为 7 的数字 7 输入一些十进制数字 -12.2 输入一些十进制数字 -10 输入一些十进制数字 -11 输入一些十进制数字 -5 输入一些十进制数字4 键入一些十进制数 5 键入一些十进制数 6 最小奇数负数 -11.000000 进程以退出代码 0 结束 我认为应该是 -12.2 而不是 11,不知道为什么!再次感谢您!
    • @Eno 该程序应该找到最小的负奇数。我可以说 -12.2 并不奇怪。
    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多