从输入7个数字(十进制-双精度)-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 %fn", "Smallest odd negative", smallestOddNegative);
}

相关内容

  • 没有找到相关文章

最新更新