for循环的嵌套
class ChickenTest{
public static void main(String[] args){
//定义公鸡,母鸡,小鸡分别是x,y,z只
int z = 0 ;
//使用for循环,针对公鸡进行穷举
for(int x = 0 ; x <= 33 ; x ++){
//继续针对母鸡进行穷举,(2 文钱可以买一只母鸡),for循环 0,50
for(int y = 0 ; y <=50 ; y ++){
//小鸡的数量
z = 100 - x - y ;
//满足的条件
//100文钱 小鸡的数量是能被3整除
if((3*x+2*y+z/3 == 100) && (z % 3 == 0)){
System.out.println("公鸡有:"+x+"只,母鸡有:"+y+"只,小鸡有:"+z+"只") ;
}
}
}
}
}
class ForForDemo{
public static void main(String[] args){
for(int x = 0 ; x <4; x++){//外层控制行数 :4行
//循环体:就是5列*
for(int y = 0; y <5 ; y ++){ //内层控制列数:5列*
System.out.print("*") ;
}
System.out.println() ;
}
}
}
class ForForTest2{
public static void main(String[] args){
//9行9列的*,列数在变化的*
for(int x = 0 ; x <9; x++){
for(int y = 0 ; y <=x ; y++){
System.out.print("*") ;
}
System.out.println() ;
}
System.out.println("-------------------------------") ;
//99乘法表:为了保证有数据,从1开始
for(int x = 1 ; x <= 9 ; x ++){//行数
//列数也在变化
for(int y = 1 ;y <=x ; y ++){
//Java中的转义字符
// x :X任意字符 t---"制表符"
System.out.print(y+"*"+x+"="+(y*x)+"t" ) ;
}
System.out.println() ;
}
}
}
class Test2{
public static void main(String[] args){
System.out.println("满足条件的五位是:") ;
//五位数,明确范围
for(int x = 10000; x <=99999; x ++){
//确定每个位上的数据本身
int ge = x % 10 ;
int shi = x /10 % 10;
int bai = x /10 / 10 % 10 ;
int qian = x/ 10/10/10 % 10;
int wan = x /10/10/10/10 % 10 ;
if((ge==wan) && (shi==qian) && ((ge+shi+qian+wan)==bai)){
System.out.println(x) ;
}
}
}
}
class WhileTest{
public static void main(String[] args){
//定义统计变量
int count = 0 ;
//定义起始厚度
int start = 1 ;
int end = 884800 ;
//满足条件
while(start < end){
//count++
count ++ ;
//起始厚度是之前的2倍
start *=2 ;
System.out.println("第"+count+"次,起始厚度是"+start) ;
}
System.out.println("需要折叠"+count+"次") ;
}
}
跳转控制语句
class BreakDemo{
public static void main(String[] args){
//break ;//在 switch 或 loop 外部中断
//写for循环
for(int x = 0 ; x <10 ; x ++){
if(x == 3){
break ;//结束,中断
}
System.out.println(x) ;//0,1,2
}
System.out.println("--------------------------") ;
//早期加入标签名称 (了解)
wc:for(int x = 0 ; x < 4 ; x ++){//行数
nc:for(int y = 0 ; y < 5 ; y ++){//列数
if(y == 2){
break nc ;
}
System.out.print("*") ;
}
System.out.println() ;
}
}
}
class ContinueDemo{
public static void main(String[] args){
//for循环
for(int x =1 ; x <= 10 ; x ++){
//加入条件
if(x ==3 ){
//break ; //结束
continue ;//结束当前循环,立即进入下一次循环了
}
System.out.println(x) ;// break ;1,2
//contine: 1,2,4,5,6,7,8,9,10
}
}
}
class ReturnDemo{
public static void main(String[] args){
System.out.println("程序开始了...") ;
for(int x = 1 ; x <= 10 ; x ++){
if(x == 3){
System.out.println("条件成立了,进入if了") ;
//break ;//当x=3的时候,成立,中断循环了结束了
//continue ;//当x=3的时候,成立,立即进入下一次循环了
return ; //结束方法去使用,很少单独使用;
}
System.out.println(x) ;
}
System.out.println("程序结束了,over...") ;
}
}
方法
有具体返回值类型的方法
class FunctionDemo{
public static void main(String[] args){
//键盘录入或者直接现在给具体值:实际参数
int a = 10 ;
int b = 20 ;
//调用方法
//单独调用:用不了,计算出来---但是没有结果出来
//add(a,b) ;
//输出调用:不建议:硬编码
//---直接输出结果,可能想通过获取的结果继续进行其他需求,用不了
//System.out.println("两个数据的之和是:"+add(a,b)) ; //a=10,b=20
//有具体返回值类型的方法的调用---推荐使用 赋值调用
//赋值调用:推荐
int result = add(a,b) ;
System.out.println("两个数据的结果是:"+result) ;
}
public static int add(int x,int y ){//10,20 :实际参数将结果赋值形式参数x,y
//赋值运算
int result = x + y ;
return result ;//具体业务结果
//return x+y ;
}
}
class FunctionDemo2{
public static void main(String[] args){
//定义求和方法:两个数据求和
//以后键盘录入
int a = 10 ;
int b = 20 ;
//赋值调用
//int result = add(int a,int b) ; //不能在携带数据类型
int result = add( a, b) ;
System.out.println("result:"+result) ;
}
//方法和方法平级关系
//定义求和方法:两个数据求和
public static int add(int a,int b){ //形式参数不影响
//public static int add( a, b)//不行,Java语言不行,语法结构和格式非常严谨!
int c = a + b ;
return c ;
}
}
//导包
import java.util.Scanner ;
class FunctionTest{
public static void main(String[] args){
//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
//提示并录入数据
System.out.println("请您输入第一个数据:") ;
int a = sc.nextInt() ;
System.out.println("请您输入第二个数据:") ;
int b = sc.nextInt() ;
//调用方法
//两个数据是否相等
boolean result = compare(a,b) ;
System.out.println("result:"+result) ;
System.out.println("-------------------------------") ;
//提示并录入数据
System.out.println("请您输入一个数据:") ;
int m = sc.nextInt() ;
System.out.println("请您在输一个个数据:") ;
int n = sc.nextInt() ;
//调用方法
int max = getMax(m,n) ;
System.out.println("两个数据中的最大值是:"+max) ;
}
public static boolean compare(int a , int b){ //形式参数,完成方法的业务逻辑
//三元
//boolean flag = (a==b)? true :false ;
//优化
boolean flag = a==b ;
return flag ;
}
public static int getMax(int a,int b){
int max =0; //结果变量
if(a != b){
//嵌套判断
if(a > b){
max = a ;
}else{
max = b ;
}
}else{
System.out.println("两个数据一致") ;
max = a ;
}
return max ;
}
}