I am asked to write a homework where I have to ask the user to type a number that is at least 7 that determines the number of doubles that they have to type after, for the first input I have to use a while loop and then to check the double decimal numbers I have to use a for loop so I can ask the user to type those 7 doubles. In the end I have to show how many numbers are typed in total, and which of these numbers was the smallest negative odd number. Thank you in advance!
For now my code looks like this:
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*/
}
}
}
If I got you right:
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);
}