The idea is to show a ladder at a given height, but a number of conditions must be met.

import java.io.IOException; import java.util.Scanner; //TODO: add Exceptions public class Mario { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); int nHeight, step, space, sharp, a = 1, b; boolean integer; do { System.out.print("Enter height of pyramid: "); nHeight = scanner.nextInt(); integer = scanner.hasNextInt(); } while ( (!integer) || (nHeight < 1) || (nHeight > 23)); b = nHeight - 2; for(step = 0; step < nHeight; step++) { for(space = 0; space < nHeight - a; space++) { System.out.print(" "); } for(sharp = 0; sharp < nHeight - b; sharp++) { System.out.print("#"); } a++; b--; System.out.println(); } } } 

One of the conditions is that something entered must be an integer number. I decided to check using the integer variable in the do while loop. However, the program does not work correctly, that is, the result is issued after several attempts to enter and it does not matter whether restrictions are passed or not.

Incorrect result of the program

But if the 17th line is removed (and the condition! Integer), then the program works as it should according to the other two conditions. What is wrong with this line? Why she does not give the desired result? How to fix this?

Closed due to the fact that the participants are off topic : , aleksandr barakin , pavel , user207618, user194374 17 Aug '16 at 5:44 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Yuriy SPb, aleksandr barakin, Spirit of the community, Spirit of the community
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • five
    Add your program code as text to the start message. - s8am
  • 6
    On this resource, it is not customary to upload screenshots of the code, provide the source code in the text and use formatting. - Dmitriy Nail

1 answer 1

The problem is integer = scanner.hasNextInt(); after you used scanner.nextInt(); . At the input of the scanner is no longer any data. Remove this check.