Java中怎样让一个程序结束后自动重新运行该程序

Java中怎样让一个程序结束后自动重新运行该程序,第1张

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函数中可以实现你想要的效果

最通俗的就是

while(true) { //用户输正确就跳出循环 }

  Scanner csc = new Scanner(System.in)

  boolean f = true

  while(f){

   int ip = csc.nextInt()

   if(ip < 999 && ip > 100){

    //输入正确

    f = false

   }

  }

你的程序有一些小错误,我给你改了,并且按照你的要求在输入错误的情况下,程序能够重新运行,你看一下吧。输入-1程序停止。

import java.util.*

class Test

{

public static void main(String[]args)

{

int month=0,seat=0

try{

Scanner s=new Scanner(System.in)

System.out.println("输入月份:")

month =s.nextInt()

System.out.println("输入舱位,头等舱为1,经济舱为2:")

seat=s.nextInt()

}catch(Exception e){

System.out.println("请输入整数!")

}

while(seat!=-1&&month!=-1){

if(month>=4&&month<=10)

{

if(seat==1)

System.out.println("您的机票价为4500RMB")

else if(seat==2)

System.out.println("您的机票价为4000RMB")

else

System.out.println("您输入的舱位代号有误")

}

else if(month>0&&month<4 || month>10&&month<=12)

{

if(seat==1)

System.out.println("您的机票价为2500RMB")

else if(seat==2)

System.out.println("您的机票价为2000RMB")

else

System.out.println("您输入的舱位代号有误")

}else{

System.out.println("您输入的月份有误")

}

try{

Scanner s=new Scanner(System.in)

System.out.println("输入月份:")

month =s.nextInt()

System.out.println("输入舱位,头等舱为1,经济舱为2:")

seat=s.nextInt()

}catch(Exception e){

 System.out.println("请输入整数!")

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7867899.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存