try-catch不能用在循环中吗
import java.util.Date;
import java.util.Scanner;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class test {
public static void main(String[] args) {
String ss;
String[] record;
int temp_month,i;
Calendar calender = Calendar.getInstance(); //////建立Calender对象 从而能直接取月份等等
Scanner scanner = new Scanner(System.in);
System.out.println("请问要输入几条记录?");
int amount = scanner.nextInt();
record = new String[amount];
for(i=0; i<amount; i++){ /////读入amount个字符串
record[i] = scanner.nextLine();
}
Date date = new Date();
SimpleDateFormat ma2=new SimpleDateFormat("yyyy/MM/dd"); //设定一种日期形式
for(i=0; i<amount;i++){ ///对record中的amount个字符串进行处理
try{
date = ma2.parse(record[i]); /////把字符串转成ma2型的Date对象date
System.out.println(ma2.format(date)); ////输出字符串“2017/05/01”
calender.setTime(date); //////把date对象da转化为Calender对象 然后就可以取月份
}
catch(Exception e){
System.out.println("no!!");
}
temp_month = calender.get(Calendar.MONTH) + 1;
System.out.println(temp_month);
}
}
}
相关推荐
JS循环跳出方法[代码]
本文详细介绍了JavaScript中跳出循环的多种方法,包括break、continue、return、throw等。文章首先解释了break和continue的基本用法,然后探讨了forEach和map循环中如何跳出循环的问题。接着,文章介绍了如何使用try-catch-throw强制退出循环,并提供了嵌套循环中跳出多层循环的解决方案,如使用return、标签语句outerLoop等。最后,文章总结了数组中可以提前结束迭代的方法,如every、some、find等,并给出了在实际开发中的使用建议。
面试官问我 ,try catch 应该在for循环里面还是外面?
try catch 应该在for循环里面还是外面
JS异步错误捕获的一些事小结
主要给大家介绍了关于JS异步错误捕获的一些事,文中通过示例代码介绍的非常详细,对大家学习或者使用Javascript具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
JS跳出forEach循环[代码]
本文详细探讨了在JavaScript中如何跳出forEach循环的问题。首先介绍了常见的循环控制关键字return、continue和break的区别及使用场景,指出break和continue在普通循环中有效,但在forEach循环中会报错。接着解释了原因:forEach的每次循环实际上是一个匿名函数,因此使用break或continue会报错,而return false只能截断本次循环匿名函数下方的代码执行。文章提供了两种解决方案:一是改用普通for循环以便使用break或continue;二是利用try...catch在特定时机抛出异常以达到跳出循环的效果。最后强调了记录知识点的重要性。
C#面向对象考试题目
非常齐全的C#考试题目 在C#程序中,下列用来处理异常的结构,错误的是( )。 a) catch{}finally{} b) try{}finally{} 在C#中,下列变量定义与赋值正确的是( )。 a) int age=12.5; b) string name = sodi; 阅读以下代码,判断下划线处填入代码( ),运行后无任何问题发生。 public class Person { private string name; public string Name{ get{return name;} set{name = value;} } private int age; public int Age { get{return age;} } } public class Test { public static void Main() { Person p = new Person(); ______________________________ } } a) p.name = "jack "; b) p.Age = 18; 在C#中,下列代码的运行结果是( )。 int[]age=new int[]{16,18,20,14,22}; foreach(int i in age){ if(i>18) continue; Console.Write(i.ToString()); } a) 16 18 20 14 22 b) 16 18 14 22 在C#中,下列代码的运行结果是( )。 using System; class Text{ static void Main(){ try{ int a=10; int b=0; int c=a/b; Console.WriteLine(c); } catch{ Console.WriteLine(“出现错误”); } finally{ Console.WriteLine(“运行结束”); } } } a) 0 运行结束 b) 10 运行结束


Eclipse
