JAVA流程控制

时间:2022-06-10 03:49:04

JAVA流程控制

example1:

--输出九九乘法表

public class chengfabiao {

public static void main (String[] args){

                                         int i = 1;

                                         int j = 1;

                                         for(i<=9;i++){

                                               for(j<=9;j++){

                                                    if(i>j){

                                                              system.out.print('        ');--打印8个空格使得输出内容格式形成一个上三角;

                                                                }

                                                     else{

                                                              syetem.out.printf('%d*%d=%2d',i,j,i*j);

                                                              }}

                                                 system.out.printf('\n');        

}}

example2:

--判断月份天数

import java.util.*;                                                                                         -------代表导入util包下的所有类

public class panduan{

           public static void main(String[] args){

                                  int days = 0;

                                 Scanner.sc =new Scanner(System.in);                    -------Scanner 是util包下的一个类,sc是一个实例或者说是对象!new 是一个创建对象的方法…… Scanner这个类最实用的地方表现在获取控制台输入

                                 System.out.println('输入年份:');

                                 int year = sc .nextInt();                                           ---------获取键盘的输入: 如果执行到了这一行,那么程序会暂停,等待你在控制台输入,然后把输入的整数值赋给整形变量 year

                                 System.out.println('输入月份:');

   int month = sc .nextInt();

                                switch(month){

                                                         case 1:

                                                         case 3:

                                                         case 5:

                                                         case 7:

                                                         case 8:

                                                         case 10:

                                                         case 12:

                                                                      System.out.println('本月有31天');   

                                                                      break;

                                                         case 4:

                                                         case 6:

                                                         case 9:

                                                         case 11:

                                                                      System.out.println('本月有30天');   

                                                                      break;

                                                         case 2:

                                                                    if(year%4==0  && year%100!=0  || year%400==0)

                                                                    {System.out.println('本月有29天');  }

                                                                     else

                                                                     {System.out.println('本月有28天');  }

                                                                      break;

                                                       default:

                                                                    System.out.println('月份输入错误');  

                                                                     System.exit(0);                                                        ---------  强制结束程序

                                                       } } }

备注:

switch (表达式){  case 值1 : 语句1 break;  case 值2 : 语句2 break;  ...  default : 语句n break;  }       

Java.util包中包含的类:

Java.util.ArrayDeque类
Java.util.ArrayList类
Java.util.Arrays类
Java.util.BitSet类
Java.util.Calendar类
Java.util.Collections类
Java.util.Currency类
java.util.Date类
java.util.Dictionary类
java.util.EnumMap类
java.util.EnumSet类
java.util.Formatter类
java.util.GregorianCalendar类
java.util.HashMap类
java.util.HashSet类
java.util.Hashtable类
java.util.IdentityHashMap类
java.util.LinkedHashMap类
java.util.LinkedHashSet类
java.util.LinkedList类
java.util.ListResourceBundle类
java.util.Locale类
java.util.Observable类
java.util.PriorityQueue类
java.util.Properties类
java.util.PropertyPermission类
java.util.PropertyResourceBundle类
java.util.Random类
java.util.ResourceBundle类
java.util.ResourceBundle.Control类
java.util.Scanner类
java.util.ServiceLoader类
java.util.SimpleTimeZone类
java.util.Stack类
java.util.StringTokenizer类
java.util.Timer类
java.util.TimerTask类
java.util.TimeZone类
java.util.TreeMap类
java.util.TreeSet类
java.util.UUID类
java.util.WeakHashMap类
java.util.Interfaces接口
java.util.Exceptions接口
java.util.Formatter.BigDecimalLayoutForm接口