最通俗的就是
while(true) { //用户输正确就跳出循环 }Scanner csc = new Scanner(System.in)
boolean f 绝腔= true
while(f){
int ip = csc.nextInt()
if(ip < 999 && ip >并数衫 100){
//输入正确
f = false
毕慎 }
}
import java.io.BufferedReader
import java.io.InputStreamReader
public class IoDemo14
{
public static void main ( String[] args ) throws Exception
{
// 定义标记位:表示当前输入的是第几个数字
// 0表示要输入第一个,1表示要输入第二个
int where = 0
int i1 = 0
int i2 = 0
String str = null
BufferedReader buf = new BufferedReader (new InputStreamReader (System.in))
// 定义一个死循环,但是里面用break改变逻辑,不再死
while (true)
{
try
{
// 输入第一个数字
if (where == 0)
{
System.out.println ("输入第一个数字:")
str = buf.readLine ()
i1 = Integer.parseInt (str)
where = 1
}
// 输入第二个数字
if (where == 1)
{
System.out.println ("轮御输入第二个数字:汪码")
str = buf.readLine ()
i2 = Integer.parseInt (str)
}
System.out.println ("两数相加之和腊陵岩等于:" + ( i1 + i2 ))
// 关闭读入流
buf.close ()
// 跳出循环
break
}
// 捕获字符串转数字时的数字格式化异常
catch (NumberFormatException e)
{
System.out.println ("输入数据不是数字类型数据请您更改 !")
// 如果异常发生,结束当前循环,返回继续
continue
}
}
}
}
// 回答完毕,采纳即可。
String flag = "n"Scanner input = new Scanner(System.in)do{System.out.println("请输入数字1:")int a = input.nextInt()System.out.println("请输入数字2:")int b = input.nextInt()System.out.println(a+"+"+b+"="+(a+b))System.out.println("是否继续?(y/n)")flag = input.next()}while(flag.equals("y"敏大茄))将此放入main函数中桥察可以实现仿族你想要的效果欢迎分享,转载请注明来源:内存溢出
评论列表(0条)