zoukankan      html  css  js  c++  java
  • Java学习第二天

    一、两个常用工具

    1、随机生产一个[0,1)之间的数

    Math.random()

    举例:

    double box =Math.random();

    //产生随机小数 [0,1)

    System.out.println("box="+box);

    //[min,max+1)

    int min =5;

    int max =9;

    box =Math.random()*(max-min+1) +min;                

    System.out.println(box);

    2、与客户交互,获取客户电脑的输入值

    java.util.Scanner

    具体步骤:

    1、定位包:import java.util.Scanner

          

    2、准备: Scanner tool =new Scanner(System.in)

         

    3、使用:tool.nextInt() 、 tool.nexDouble

    举例:

    import java.util.Scanner;

    public class ToolDemo02{

             public static void main(String[] args){

                       //2、准备工具

                       Scanner tool =new Scanner(System.in);

                       //3、使用

                       //1)、获取整数

                       System.out.println("请输入年龄:");

                       int age = tool.nextInt();

                       System.out.println("你输入的年龄为:"+age);

                      

                       //2)、获取小数

                       System.out.println("请输入海拔:");

                       double height =tool.nextDouble();

                       System.out.println("你输入的海拔为:"+height);

     

    二、类型转换

    造成的原因,数据类型不匹配,为了匹配问题,需要对类型进行处理,分类为

    自动类型转换

    大类型盒子 = 小类型的数据

    1、  如果存在大数参与运算,结果以大数为准,尽可能将大数提前。

    2、  在表数范围内: byte short char =字面值|常量值

    3、  算术时  :

    1)、字面值|常量值

    byte| short| char|int =byte short char  int +byte short char  int

    2)、一旦有变量参与运算

    int =byte short char  int +byte short char  int

    强制类型转换

    小类型盒子 = (小类型)大类型的数据;

    注意() 作用范围

    举例:

    /**
    强制类型转换: 小类型变量 =(小类型)大类型的值
    注意点: ()作用范围
    */
    public class ParseDemo02{
    public static void main(String[] args){
    char ch ='A';
    int box = ch; //自动
    ch =(char) box; //强制

    double num = 3.5;
    box =(int)num; //舍弃小数点
    System.out.println(box);

    //()作用范围
    box = (int)(num*2); //(int)num*2
    System.out.println(box);

    //产生[0,8)随机整数
    int max =8;
    int min =0;
    int letter =(int)Math.random()*(max-min)+min;
    System.out.println(letter);
    //正确写法
    letter =(int)(Math.random()*(max-min)+min);
    System.out.println(letter);

    //产生随机的小写字母 大写字母
    //97+0-->'a'
    //97+1-->'b'
    //97+25-->'z'
    max ='z'+1;
    min =97;
    letter =(int)(Math.random()*(max-min)+min);
    System.out.println((char)letter);
    //65 -->'A'

    //产生小写字母 (根据已有的工具 解决未知的问题)
    char lowerLetter =(char)(Math.random()*('z'-'a'+1)+'a');
    System.out.println(lowerLetter);
    //思考如何产生大写字母

    }

    }

    三、选择执行

    最多执行一次

    1、if :条件boolean值,一般区间

    “如果就”,条件成立执行,分为三类

    1)、单选择

    If(条件){

      //执行代码块

    }

    举例:

    /**
    单选择:
    if(){
    }

    产生[0,100] 整数,判断是否为偶数,如果是输出偶数
    */
    public class IfDemo01{
    public static void main(String[] args){
    //1、产生[0,100] 整数
    int rnd =(int)(Math.random()*101);
    System.out.print("产生的数为:"+rnd);

    //2、判断是否为偶数,如果是偶数,输出"此数是偶数"
    //分析: 偶数 rnd%2==0
    /*
    boolean flag = (rnd%2==0);
    //套用选择
    if(flag){
    System.out.println("此数为偶数");
    }
    */

    //在开发时如果变量只使用一次,可以匿名
    if(rnd%2==0){
    System.out.println("此数为偶数");
    }

    }

    }

    2)、双选择

    If(条件){

      //执行代码块1

    }else{

    //执行代码块2

    }

    举例:

    /**
    双选择:
    if(){
    }else{
    }
    产生[0,100] 整数,判断是否为偶数,如果是输出偶数,否则输出奇数
    */
    public class IfDemo02{
    public static void main(String[] args){
    //1、产生[0,100] 整数
    int rnd =(int)(Math.random()*101);
    System.out.print("产生的数为:"+rnd);

    //2、判断是否为偶数,如果是偶数,输出"此数是偶数"
    //在开发时如果变量只使用一次,可以匿名
    if(rnd%2==0){
    System.out.println("此数为偶数");
    }else{
    System.out.println("此数为奇数");

    3)、多选择

    If(条件1){

      //执行代码块1

    }else if(条件2){

    //执行代码块2

    }else if(条件3){

    //执行代码块3

    }else{

    }

    举例

    /**
    多选择: 从键盘输入 [1,7] 的整数
    1、判断是否为周末或工作日
    */
    import java.util.Scanner;
    public class IfDemo03{
    public static void main(String[] args){
    Scanner tools =new Scanner(System.in);

    //从键盘输入1-7 整数
    int day =tools.nextInt();
    if(day==1){
    System.out.println("悲催工作日");
    }else if(day==2){
    System.out.println("悲催工作日");
    }else if(day==3){
    System.out.println("悲催工作日");
    }else if(day==4){
    System.out.println("悲催工作日");
    }else if(day==5){
    System.out.println("悲催工作日");
    }else if(day==6){
    System.out.println("无聊的周末");
    }else if(day==7){
    System.out.println("无聊的周末");
    }
    System.out.println("好无生趣的人生");

    if(day>=1 &&day<=5){
    System.out.println("悲催工作日");
    }else if(day>=6 && day<=7){
    System.out.println("无聊的周末");
    }else{
    System.out.println("错误的输入。。。");
    }


    }

    }

    2、switch:定值判断

    结构

    switch(int整数|1.7字符串|枚举){

           case 定值1(字面值|常量) :

                //代码1

                //break;

           case定值2(字面值|常量) :

                //代码2

                //break;

    ……

           default :

    //其他情况

    }

    需注意:

    1、  switch 定值表达式

    2、  case 定值:常量字面值枚举值

    3、  default: 一般放在最后,位置不固定

    4、  break: 结束switch, 防止下穿

    结束:  正常到 }  遇到break

    举例

    import java.util.Scanner;

    public class SwitchDemo03{

             public static void main(String[] args){              

                       Scanner tool =new Scanner(System.in);          

                       //接收月份[1,12]

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

                       int month =tool.nextInt();

             switch(month/3){

                                case 1:                       

                                         System.out.println("春困");

                                         break;

                                case 2:                       

                                         System.out.println("夏打盹");

                                         break;

                                case 3:                       

                                         System.out.println("秋乏");

                                         break;

                                default:

                                         System.out.println("冬眠");

                       }

                      // 上面为简单形式,下面为复杂形式。

                       /*

                       switch(month){

                                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;

                                default:

                                         System.out.println("冬眠");

                       }

                       */

                      

             }

    }

  • 相关阅读:
    使用NDK编译 libyuv <转>
    x264中重要结构体参数解释,参数设置,函数说明 <转>
    x264的一些参数设置对编码效率的影响
    首都儿研所开钙片!!!
    Android 媒体编解码器(转)
    opengl版本和扩展
    ffmpeg一揽子
    Android 使用SWIG生成Jni代码<转>
    CF 19D 线段树+set压缩坐标轴+离散化map
    android4.0 FaceDetection笔记
  • 原文地址:https://www.cnblogs.com/yuyufeng/p/5293620.html
Copyright © 2011-2022 走看看