A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Zengpenh 中级黑马   /  2018-1-9 17:33  /  1091 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学习经历:
1、for循环语句、whlie循环语句、do while循环语句的练习
2、break、continue的应用
3、Scanner类和Random类的应用
/**
* 几种循环语句格式
*
*
* */
public class ForTest1 {
public static void main(String[] args) {

//for循环
//要求输出打印1-10
System.out.print("[");
for(int a=1;a<=10;a++){
  if(a==10){
   System.out.println(a+"]");
  }else
  System.out.print(a+",");
}
System.out.println();

//要求输出1-100的和
int sum=0;
for(int x=1;x<=100;x++){
  sum=sum+x;
  System.out.println(x+"----"+sum);//帮助理解执行流程
}
System.out.println("sum="+sum);
/* 无限循环格式
  * for( ; ; ){
  *   System.out.println(无限循环);
  * }
  *
  *
  * */

//要求打印1-100中奇数的个数
int count=0;
for(int x=1;x<=100;x++){
  if(x%2!=0)
   count++;
}
System.out.println("1-100中有"+count+"个奇数");


//while循环
/*while(true){
}//无限循环
do{

}while();*/
}
}




2、
/**for循环语句
* for(初始化表达式;判断条件表达式;++或者--){}
* */
public class ForTest2 {
public static void main(String[] args) {
//在控制台输出10次HelloWorld的案例
for(int a=0;a<10;a++){
  System.out.println("HelloWorld");
}
//求出1-10之间数据的和
int sum=0;
for(int a=1;a<=10;a++){
  sum=sum+a;
}
System.out.println("1-10之间的和为:"+sum);

//求出1-100之间的偶数和
int sum1=0;
for(int x=1;x<=100;x++){
  if(x%2==0){
   sum1=sum1+x;
  }
}
System.out.println("1-100之间的偶数和为:"+sum1);

/*在控制台输出所有的水仙花数
举例:153就是一个水仙花数
  153=1*1*1+5*5*5+3*3*3
  并且统计水仙花出现的次数
  */
//求次幂:
/*double s=Math.pow(a, b)
System.out.println(s);*/
int count=0;//定义一个变量记录住出现的次数
for(int a=100;a<1000;a++){
  int x=a%10;//个位
  int y=(a%100)/10;//十位
  int z=a/100;//百位
  if(a==(x*x*x)+(y*y*y)+(z*z*z)){
  System.out.print(a+" ");
   count++;
  }
}
System.out.println();
System.out.println("水仙花的个数有"+count+"个");


}

}




3、
public class ForTest3 {
public static void main(String[] args) {
  /*练习1:
   * 1,打印1-100之内的整数,但数字中包含9的要跳过
   * 2,每行输出5个满足条件的数,之间用空格分隔
   * 3,如:1 2 3 4 5
   *   6 7 8 9 10
   * */
  int count = 0;
  for (int b = 1; b <= 100; b++) {
   if (b % 10 == 9 || b % 100 / 10 == 9) {
    continue;
   }
   count++;
   if (count % 6 == 0) {
    System.out.println();
   } else {
    System.out.print(b+" ");
   }
  }System.out.println();
  /*1,按照从大到小的顺序输出四位数中的个位+百位=十位+千位(3553,2332,1166,8228,3773)
   * 的数字及个数
   * 2,每行输出5个满足条件的数,之间用空格隔开
   * 3,如9999,9988,9977,9966,9955
   * */
  
  int count1 = 0;
  for (int a = 9999; a >= 1000; a--) {
   /* int i=6345;
    System.out.println(i%10);个位
    System.out.println((i%100)/10); 十位
    System.out.println((i%1000)/100);百位
    System.out.println(i/1000);千位
    * */
   int ge = a % 10;// 个位
   int shi = (a % 100) / 10;// 十位
   int bai = (a % 1000) / 100;// 百位
   int qian = a / 1000;//千位
   if (ge + bai == shi + qian) {
    count1++;
    System.out.print(a + " ");
    if (count1 % 5 == 0) {
     System.out.println();
    }
   }
  }
  System.out.println("从大到小的顺序输出四位数中的个位+百位=十位+千位的个数有:" + count1);
  
  //打印乘法表
  int a=9;
  int chen=0;
  for(int x=1;x<=a;x++){
   for(int y=1;y<=x;y++){
    chen=x*y;
    System.out.print(y+"*"+x+"="+chen+" \t");
   }
   System.out.println();
  }
}
}



4、

/**
* break的作用是结束单层循环 break:跳出的意思
*
* continue的作用是结束当前的单次循环
*/
public class ForTestBreak {
public static void main(String[] args) {
  // TODO Auto-generated method stub
  // break的使用
  for (int i = 0; i < 10; i++) {
   System.out.println(i);
   if (i == 5) {
    break;// 跳出i==5这层循环
   }
  }
  System.out.println("程序结束");
  // 现在做个数数的游戏,让计算机数数数到1000000,然后让他停止,记录一共用了多少时间
  long start = System.currentTimeMillis();// 获取系统当前时间
  int a = 0;
  while (true) {
   // System.out.println(a++);
   a++;
   if (a == 1000000) {
    break;
   }
  }
  long end = System.currentTimeMillis();// 获取系统当前时间
  long time = end - start;
  System.out.println(time);
  /*
   * for (int i = 0; i < 10000000; i++) { if(){} }
   */
  // 整理代码格式的快捷键ctrl+shift+f
  // continue的使用:数数数到20,遇到带9的数字,跳过
  for (int b = 1; b <= 20; b++) {
   if (b % 10 == 9) {
    continue;
   }
   System.out.println(b);
  }
}
}



5、
package com.itheima.firstdemo4;
/**
* Random类的使用 获取一个随机数
* Scanner键盘输入的使用
**/
import java.util.Random;
import java.util.Scanner;
/*第一步  导包 随机数
import java.util.Random;
*/
/*第1步  导包 键盘输入
import java.util.Scanner;
*/
public class ForTestRandom {
public static void main(String[] args) {
  /*
   * Random 第二步:创建一个对象 Random b= new Random();//引用型数据变量 第三步:获取一个随机数 int a
   * = b.nextInt(100); //[0,100) System.out.println(a);
   */
  /*
   * Scanner 第2步 创建对象 Scanner scanner = new Scanner(System.in); 得到输入的数据
   * int c = scanner.nextInt(); System.out.println(c);
   */
  /*
   * 猜数字小游戏案例 系统产生一个1-100之间的随机数,请猜出这个数据是多少。并记录一共猜了多少次
   */
  Random a = new Random();// 第二步:创建一个对象
  int b = a.nextInt(100) + 1;// 获取一个数
  // System.out.println(b);//打印获取到的随机数
  Scanner c = new Scanner(System.in);
  System.out.println("猜数字小游戏启动");
  int count = 0;//定义一个变量记录猜的次数
  System.out.println("猜数字小游戏开始");
  while (true) {//无限循环
   int d = c.nextInt();// 获取到键盘输入得数
   count++;//猜的次数++
   if (d > b) {//如果输入的数大于随机产生的数
    System.out.println("您猜大了请继续猜");
   } else if (d < b) {//如果输入的数小于随机产生的数
    System.out.println("您猜小了请继续猜");
   } else {
    System.out.println("恭喜您猜对了");
    break;//如果答案正确结束无限循环
   }
  }
  System.out.println("您总共猜了" + count + "次");
}
}



5 个回复

倒序浏览
期待继续更新
回复 使用道具 举报
期待继续更新
回复 使用道具 举报
哇,厉害了哟,继续加油!
回复 使用道具 举报

期待继续更新~~~
回复 使用道具 举报
期待你每天的进步   温故而知新
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马