-1

Write a program that continuously prompts the user to enter integers from the keyboard. The program terminates when the integer entered is –5 or 0 or is greater than 8. Use logical ‘and’ in your loop control condition. Test your program carefully to ensure that all the loop termination criteria are met.

import java.util.Scanner;

public class ques {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner (System.in);
    int num;

    System.out.print("Enter n (-5 or 0 to stop):");
    do {
        num = input.nextInt();

        if ((num!=-5) && (num!=0) && (num>8)){
            System.out.println("Integers: "+num);
        }
    }while ((num!=-5) && (num!=0) && (num>8)); {
        System.out.println("Integers:" +num);
    }


}

}

Started out with this but couldn't complete it. Help.

Tuff
  • 19
  • 6

3 Answers3

1

If you don't want to have problems with Scanner you have to add an input.nextLine() without storing the value after input.nextInt()

import java.util.Scanner;
    public class ques {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner (System.in);
    int num;

    System.out.print("Enter n (-5 or 0 to stop):");
    do {
        num = input.nextInt();
        input.nextLine();

        if ((num!=-5) && (num!=0) && (num>8)){
            System.out.println("Integers: "+num);
        }
    }while ((num!=-5) && (num!=0) && (num<=8)); {
        System.out.println("Integers:" +num);
    }  
}
}

If you want to know why do you have to put input.nextLine() you can see this answer that I posted some months ago: Why isn't the scanner input working?

Also, in your while condition you have to put num<=8 instead of num>8.

I expect it will be helpful for you!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
0

You mixed up the last condition, it should be num<=8, instead of num > 8.

Your loop should continue until one of these conditions are met:

  • num == -5
  • num == 0
  • num > 8

This is equivalent to continue while:

  • num != -5
  • num != 0
  • num <= 8

As you can see, the last expression in the condition you wrote is wrong, it should be num <= 8, instead of num > 8.

amit
  • 175,853
  • 27
  • 231
  • 333
0

In your if statement you are saying if the input number is not -5, not 0 or greater than 8.

This is your error:

Change num>8 to num<=8

working code:

import java.util.Scanner;
public class ques 
{

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        Scanner input = new Scanner (System.in);
        int num;

        System.out.print("Enter n (-5 or 0 to stop):");
        do 
        {
            num = input.nextInt();

            if ((num!=-5) && (num!=0) && (num<=8))
            {
                System.out.println("Integers: "+num);
            }
        }while ((num!=-5) && (num!=0) && (num<=8)); 
        {
            System.out.println("Integers:" +num);
        }
    }
}
AnonDCX
  • 2,501
  • 2
  • 17
  • 25