成都创新互联网站制作重庆分公司

java重新运行的代码 java重载代码

关于JAVA,怎么返回重新执行

最通俗的就是

网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了桂阳免费建站欢迎大家使用!

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

Scanner csc = new Scanner(System.in);

boolean f = true;

while(f){

int ip = csc.nextInt();

if(ip  999  ip  100){

//输入正确

f = false;

}

}

java程序捕捉异常后怎么重新执行程序

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;

}

}

}

}

// 回答完毕,采纳即可。

java怎么返回重新运行?

你的程序有一些小错误,我给你改了,并且按照你的要求在输入错误的情况下,程序能够重新运行,你看一下吧。输入-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=4month=10)

{

if(seat==1)

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

else if(seat==2)

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

else

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

}

else if(month0month4 || month10month=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("请输入整数!");

}

}

}

}


文章题目:java重新运行的代码 java重载代码
当前地址:http://cxhlcq.com/article/ddpodic.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部