程序流程控制:
顺序结构
程序从上到下逐行的执行,中间没有任何的判断和跳转
分支结构
>根据条件,选择性的执行某段代码
>有if-else和switch-case两种分支语句。
if语句的三种格式:
1、if(条件表达式){
执行代码块;
}
2、if(条件表达式){
执行代码块1;
}else{
执行代码块2;
}
3、if(条件表达式){
执行代码块1;
}else if{
执行代码块2;
}
……
else{
执行代码块n;
}
如何从键盘获取不同类型的变量:需要使用Scanner类
具体实现步骤:
- 导包:import java.util.Scanner;
- Scanner实例化:Scanner input =new Scanner(System.in);
- 调用Scanner类的相关方法,来获取指定类型的变量:int cardId = input.nextInt();
循环结构
>根据循环条件,重复性的执行某段代码
>有while、do-while、for三种循环结构
①:初始循环条件
②:循环条件
③:循环体
④:迭代条件
while循环结构:
①
while(②){
③;
④;
}
do-while循环结构:
①
do{
③;
④;
}while(②);
for循环结构:
for(①;②;④){
③
}
>zhu,jdk1.5提供了foreach循环,方便的遍历集合、数组元素
判断是否是闰年
public class Demo4 {
public static void main(String[] args) {
/*使用if选择结构判断某一年份是否是闰年。
闰年的条件:
普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年);
世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年);*/
for(int i=1000;i<=2000;i++){
if(((i % 4 == 0) && (i % 100 != 0))|| (i % 400 == 0)){
System.out.println(i);
}
}
}
}
求10以内偶数的和
public class Demo {
public static void main(String[] args) {
//求10以内的偶数的和。
int sum=0;
for(int i = 1;i<=10;i++){
if(i%2==0){
sum+=i;
}
}
System.out.println(sum);
}
}
九九乘法表
public class NineNineTable {
public static void main(String[] args) {
for(int i = 1; i<=9; i++){
for(int j = 1; j<=i; j++){
System.out.print(i+"*"+j+"="+(i*j)+" ");
}
System.out.println();
}
}
}
使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
// 使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)
Scanner input = new Scanner(System.in);
System.out.print("请输入月份:");
int month = input.nextInt();
switch(month){
case 12:
case 1:
case 2:
System.out.println("冬天");
break;
case 3:
case 4:
case 5:
System.out.println("春天");
break;
case 6:
case 7:
case 8:
System.out.println("夏天");
break;
case 9:
case 10:
case 11:
System.out.println("秋天");
break;
}
}
}
实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数, 从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
/*
* 实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数, 从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
*/
int fame = 0;
int exchange = 1;
while (fame == 0) {
a:while (exchange == 1) {
int num1 = (int) (Math.random() * 35 + 1);
int num2 = (int) (Math.random() * 35 + 1);
int num3 = (int) (Math.random() * 35 + 1);
int num4 = (int) (Math.random() * 35 + 1);
int num5 = (int) (Math.random() * 35 + 1);
int num6 = (int) (Math.random() * 35 + 1);
int num7 = (int) (Math.random() * 14 + 1);
if (num1 == num2 || num1 == num3 || num1 == num4
|| num1 == num5 || num1 == num6 || num2 == num3
|| num2 == num4 || num2 == num5 || num2 == num6
|| num3 == num4 || num3 == num5 || num3 == num6
|| num4 == num5 || num4 == num6 || num5 == num6) {
continue a;
} else {
exchange = 1;
System.out.println(num1 + "+" + num2 + "+" + num3 + "+"
+ num4 + "+" + num5 + "+" + num6);
}
if (exchange == 1) {
Scanner input = new Scanner(System.in);
System.out.println("是否继续下注:Y/N");
char id = input.next().charAt(0);
if (id == 'Y') {
fame = 0;
} else if (id == 'N') {
exchange=0;
fame = 1;
System.out.println("谢谢参与");
}
}
}
}
}
}