从输入的 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);
}
我被要求写一个家庭作业,我必须要求用户键入一个至少为 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);
}