zoukankan      html  css  js  c++  java
  • java -03 三元运算符和for和switch

    import java.math.BigInteger;
    class Threeyaun{
    public static void main(String[] args){

    //_______________________________________________________________________________
    /* int age =100 ;
    if (age <=50){
    System.out.println("小");
    }
    else{
    System.out.println("大");
    }

    //
    System.out.println(age <=50 ?"大" : "小");
    int a= 1,b = 2 ,c = 3;//定义一个字符串
    int max = 0 ;
    if (a>b){
    if(a > c){
    max=a;
    }
    else{
    max = c;
    }
    }

    else{
    if(b>c){
    max =b;
    }

    else{
    max=c;
    }
    }

    System.out.println("max is "+ max);
    //用三元运算符表示的话 a ? b :c ;
    System.out.println("max is "+ (a > b ? a:b));// 如果a > b ? 取出a
    System.out.println("max2 is "+ (a > b ? (a>c ? a:c) : (b>c ? b:c)));//先判断a > b ? ,如果a>b执行(a>c ? a:c),否则: (b>c ? b:c)



    //_______________________________________
    //四个数表示大小//(a > b ? (a>c ? a:c) : (b>c ? b:c)))
    /* int a= 1,b = 5 ,c = 3 ,d=2; //a>b,判断a>c 判断(a>c ? a:c)>d?
    System.out.println("max2 is "+ (a > b ? ((a>c ? a:c)>d? (a>c ? a:c):d ) : ((b>c ? b:c)>d ?(b>c ? b:c):d ) ));

    }

    } */


    //四个数相比较的时候,比较大小

    /* class demos{
    public static void main (String [] args ){
    int a1 = 1 ,b1 =100,c1 =3, d1=4;
    //a>b?
    int max ;
    if (a1>b1){
    //a1>c1?
    if(a1>c1){
    if (a1>d1){
    max = a1;
    }
    else{
    max =d1;
    }
    }
    //a1<=c1
    else{
    //c1>1d?
    if (c1 >d1){
    max = c1;
    }
    else {
    max =d1;
    }
    }
    }
    //a1<=b1
    else{
    if (b1 >c1){
    if (b1>d1){
    max = b1;
    }
    else{
    max =d1;
    }
    }
    //b1<=c1
    else{
    if (c1>d1){
    max = c1;
    }
    else{
    max = d1;
    }
    }
    }
    System.out.println("max is" + max);
    }
    } */


    //1
    /* if (true){
    执行语句;
    }

    //2:
    if (){
    执行语句;
    }
    else{
    执行语句;
    }

    //3
    if (){
    执行语句;
    }
    else if (){

    }
    else{
    执行语句;
    }
    */



    //_______________________________________________________________________________
    //else if 定义字符串year来显示年龄区间
    /* int age1 = 30;
    String year = "";
    if (age1 >=18 && age1 <=30){
    year ="中年";
    }
    else if(age1 <18){
    year ="少年";
    }
    else if(age1 >50 &&age1 <300){
    year ="老年";
    }
    System.out.println(year);
    */


    //_______________________________________________________________________________
    /* switch(x){
    case 1:
    xxx
    break;
    case2 2:
    }
    //当x 的值为case1 输出某值
    switch有四种表示类型:
    1.byte
    2.int
    3.short
    4.char */
    /* int x =1;
    switch(x){
    case 1 :
    System.out.println("x");
    break ;
    case 2 :
    System.out.println("xx");
    break ;
    case 3 :
    System.out.println("xxX");
    break ;
    default :
    System.out.println("x?");
    break ;
    }
    */
    //用switch写出四季 int byte char 都可以 当写成string,类型的时候要string ="3",那么case "3"
    //345春,678夏,9 10 11秋季 mounth + 除以3
    //if 和switch 的结合使用

    /* int mounth =10;
    int season = (mounth+1)/3 ;
    if (mounth <1 || mounth >12){
    System.out.println("月份输入错误");
    }
    else{
    switch(season){
    case 0 :
    System.out.println("冬季");//1
    break ;
    case 1 :
    System.out.println("春季");//234
    break ;
    case 2 :
    System.out.println("夏季");//567
    break ;
    case 3 :
    System.out.println("秋季");//8910
    break ;
    case 4 :
    System.out.println("冬季");//11 12
    break ;
    }
    } */



    // ________________________________________________________________________________
    //while 循环
    /* while (exp){
    ...循环体
    } */
    /* int i = 1;
    while(i <= 10){
    System.out.println(i);
    i ++ ;

    } */

    //打印1-100的和
    /* int i = 1;
    int sum = 0;
    while(i <= 100){
    sum = sum + i ;
    i++;
    }
    System.out.println(sum);

    }

    }
    */



    // ________________________________________________________________________________
    //for 循环体 for(初始化表达式,循环表达式,循环后的操作表达式)
    /* for (int i = 0; i <10 ; i++){
    System.out.println(i);
    }

    */





    //练习题


    /* float score = 78.800f;
    String level = "";
    if (score >=0 && score <=60){
    level ="差";
    }
    else if(score >=60 && score <70){
    level ="中";
    }
    else if(score >=70 && score <80){
    level ="良";
    }
    else if(score >=80 && score <100){
    level ="好";
    }
    else{
    level ="非法值";
    }
    System.out.println(level);
    }
    }
    */


    //求1-1000的质数
    /* int j ;
    for (int i = 2; i<=1000; i++){
    boolean flag = false;
    for( j=2; j<i/2; j++){
    if (i%j == 0){
    flag = true;
    break;
    }
    }
    if (flag==false){
    System.out.println(i);
    }
    } */


    //打印10行;定义n=10行,正三角打印
    /* int n =10;
    for (int i = 1 ;i<=n ; i++){
    for (int j = 1 ;j<=i; j++){
    System.out.print("*");
    }
    System.out.println();//换行
    }
    */

    //乘法表
    /* int n =9;
    for (int i = 1 ;i<=n ; i++){
    for (int j = 1 ;j<=i; j++){
    System.out.print(j + "*" +i+ "=" +(i*j) + " ");
    }
    System.out.println();//换行
    }
    */


    //倒三角乘法表
    /* for (int i = 9;i>=1; i--){
    for (int j = 1 ;j<=i; j++){
    System.out.print(j + "*" +i+ "=" +(i*j) + " ");
    }
    System.out.println();//换行
    }

    */


    //输出空三角
    /* int n =9;//最后一行的个数
    //i从5开始倒着往上,行数
    for (int i = n/2+1 ;i>=1 ; i--){
    for (int j = 1 ;j<=n; j++){
    //当i=4时,条件1和2成立,打印第四个和第六个。当等于三的时候,第三个和第七个成立。
    if (j==i||j==n+1-i||i==1){
    System.out.print(j);
    }
    else{
    System.out.print(" ");
    }
    }
    System.out.println();//换行
    } */

    //输出2的1-64次幂的和
    /* BigInteger a = BigInteger.valueOf(2);
    BigInteger sum = BigInteger.valueOf(0);
    BigInteger a = BigInteger.valueOf(0);
    for( int i = 1 ; i <= 64 ; i++){
    a =a +sum;
    sum = sum.add(a.pow(i));
    System.out.println(a);
    }
    */


    //输出2的1-64次幂的和 每次的数乘以2
    /* int i;
    int a =1;
    int sum =0;
    for (i =0 ;i<65 ;i++){
    sum = sum +a;
    a = a*2;
    }
    System.out.println(sum);
    */


    //百钱买百鸡
    /* int i ,j,k;
    for (i=0;i<=20;i++){
    for (j=0;j<=33;j++){
    for (k=0;k<=99;k=k+3){
    if ((k+i+j ==100)&&(i*5+j*3 +k/3 ==100))
    System.out.println("大"+i+"中"+j+"小"+k);

    }
    }
    } */

    //正常思路打印空三角
    0000* 0
    000*0* 1
    00*000* 2
    0*00000* 3
    ********* 4

    int line =5;//循环的所有行
    for (int i =0 ; i<line;i++){
    //输出n个空格(0)
    for(int j =0; j<line-1-i;j++){
    System.out.print(" ");
    }
    System.out.print("*");//之后打印*
    //后半部分处理最后一行
    if (i ==(line -1)){
    for(int j = 0; j<2*i;j++){
    System.out.print("*");
    }
    }//中间行
    if ( i !=0 && i!=(line-1)) {
    //输出2n-1个空格
    for(int j =0; j<2*i -1;j++){
    System.out.print(" ");
    }
    System.out.print("*");
    }
    System.out.print(" ");
    }

    }
    }

  • 相关阅读:
    Python XPath抓取小说《三国演义》《一》
    Python 爬虫学习路径
    Python爬虫练习:抓取笔趣阁小说(一)
    import win32api 安装pip install pypiwin32
    pycharm 选中多行,点back space键无法删除
    No module named 'wx'
    python GUI wxPython
    python -m pip install --upgrade pip 失败
    停更一年了,要不要回归呢,离开了运维行业也一年了。
    zabbix使用percona的mysql监控模板监控
  • 原文地址:https://www.cnblogs.com/simly/p/9957521.html
Copyright © 2011-2022 走看看