jeudi 17 janvier 2019

How to fix my while loop as it is not outputting the values

I am trying to make a program that outputs all of the prime numbers between a user entered min and max value, but my code isn't working and I cannot find the issue.

public class PrimeNumberB {
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    int max;
    int min;
    int counter = 0;

    //Asking for input
    do {
        System.out.print("Enter a minimum value: ");
        min = input.nextInt();
        System.out.print("Enter a maximum value: ");
        max = input.nextInt();
        }
        while(min > max);
        input.close();

    int value = min;    
    int number = value;
    //Prime or not
    if(max < 2) {
        System.out.println("No prime numbers");
    }else{
        System.out.println("Prime numbers from " + min +  " to " + max + " are: ");
         }
    while(min <= max) {
        while(number >= 1) {
            if(value % number == 0) {
                counter = counter +1;
            }
            number= number - 1;
        }
        if(counter == 2) {
            System.out.println(value);
        }
        min++;
    }
}

}

I expect it to continue the while loop and output " Prime numbers from min to max are: x x x " but all I get is "Prime numbers from min to max are:"

Aucun commentaire:

Enregistrer un commentaire