如何在伪代码中编写 hasnextInt() 函数。

huangapple go评论66阅读模式
英文:

how to write hasnextInt() in Pseudocode

问题

以下是翻译好的内容:

我刚开始学习Java,需要编写一个程序来验证用户输入的整数。然而,我也需要编写一个算法来实现这一点。通常我们如何用伪代码编写hasNextInt()函数呢?
我已经写了一些类似这样的代码:

当(如果输入不是整数)
    输出 "错误,请只输入整数:"
    (你该如何编写这部分?)
结束循环
n1 := 输入

有没有什么方式可以改进这个伪代码,使其更有意义?

// 在这里输入代码
while(!read.hasNextInt())    // 检查用户输入是否为整数,如果不是整数,则执行循环
{
    System.out.print("错误,请只输入整数:");    
    read.next();    // 提示用户重新输入
}
n1 = read.nextInt();    // 从输入中获取数据并赋值给n1
英文:

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

答案1

得分: 0

伪代码有许多不同的风格。因此,只要您的代码保持一致,就有几种编写方式。

与您的伪代码保持一致,例如read.next()可以写为CONTINUE或者IGNORE INPUT

英文:

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.

huangapple
  • 本文由 发表于 2020年4月8日 17:20:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61097284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定