简体   繁体   中英

how to write hasnextInt() in Pseudocode

I am new to Java and i am required to write a program that will validate user input to integer. However, i will need to write an algorithm to it too. How do we usually write hasNextInt() in pseudo code? i have written something like:

WHILE (IF INPUT IS NOT INTEGER)
    OUTPUT "Error, enter integer only: "
    (how do you write this?)
END WHILE
n1 := INPUT

Is there anyway that i can improve this pseudocode to make it more sensible?

//enter code here
while(!read.hasNextInt())    // check if user input is an integer, if it is not integer, run the loop
{
    System.out.print("Error, enter integer only: ");    
    read.next();    // prompt user to enter input again
}
    n1 = read.nextInt();    // get the data from the input and assign into n1

Pseudocode comes in many flavors. Therefore, there are several ways to write things as long as your code stays consistent.

Staying consistent to the rest of your pseudocode, read.next() could be written as CONTINUE or as IGNORE INPUT , for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM