Java学习条记

源码 2024-10-1 08:57:22 31 0 来自 中国
自增自减运算符
短路逻辑运算符


两只老虎,用三元运算符比力巨细


public class HelloWorld {
public static void main(String[]  args)  {
  int m = 180 ;
  int n = 200 ;
  boolean b = m == n ? true : false ;
  System.out.println("b:" + b );
}
}
三个僧人


public class HelloWorld {
public static void main(String[]  args)  {
  int a =150 ;
  int b = 165 ;
  int c = 210 ;
  int zhonghight =a > b ? a : b ;
  int max = zhonghight > c ? zhonghight : c ;
 
  System.out.println("max:" + max  );
}
}
数据输入Scanner


import java . util . Scanner ;
public class HelloWorld {
public static void main (String[] args)  {
  Scanner m = new Scanner (System. in );
  int n = m . nextInt ( ) ;
  System . out . println ("n:" + n ) ;
}
}
三个僧人升级版


import java . util . Scanner ;
public class HelloWorld  {
public static void main ( String []  args)  {
  Scanner sc = new Scanner ( System . in ) ;
  int height1 = sc. nextInt ( ) ;
  int height2 = sc. nextInt ( ) ;
  int height3 = sc. nextInt ( ) ;
  int tempHeight = height1 > height2 ? height1 : height2 ;
  int maxHeight = tempHeight > height3 ? tempHeight : height3 ;
  System . out . println ( maxHeight + "cm" ) ;
}
}
if语句格式一


public class HelloWorld {
public static void main (String[]  args)  {
  System . out . println ("Begin") ;
  int a = 10 ;
  int b = 20 ;
  int c = 10 ;
  if ( a == b ) {
   System . out . println ( " a = b ") ;
  }
  if ( a == c )  {
   System . out . println ( " a = c ") ;
  }
  System . out . println ( " Over ") ;
}
}
if else语句


public class HelloWorld {
public static void main (String[]  args)  {
  System . out . println ("Begin") ;
  int a = 10 ;
  int b = 20 ;
  if ( a == b ) {
   System . out . println ( " a = b ") ;
  }
  else {
   System . out . println ( " a/=b ") ;
  }
  System . out . println ( " Over ") ;
}


用if else区分奇数偶数
import java . util . Scanner ;
public class HelloWorld  {
  public static void main ( String []  args )  {
Scanner sc = new Scanner ( System . in ) ;
System . out . println (  "QingShuRuYiGeZhengShu " ) ;
int number  = sc . nextInt ( ) ;
if ( number % 2 == 0 ) {
  System . out . println ( " OuShu ") ;
          }  else {
  System . out . println ( "JiShu " ) ;
}
       
  }
if语句三




import java . util . Scanner ;
public class HelloWorld  {
public static void main (String [ ]  args) {
  System . out . println ("Begin") ;
  Scanner sc = new Scanner (System . in ) ;
  System . out . println ("ShuRuYiGeXingQiShu") ;
  int week = sc . nextInt ( ) ;


  if ( week == 1 )  {
   System . out . println ("ZhouYi");
  } else if (week == 2) {


    System . out . println ("ZhouEr");


  } else if (week == 3) {


   System . out . println ("ZhouSan");


  } else if (week == 4) {


   System . out . println ("ZhouSi");


  } else if (week == 5) {


   System . out . println ("ZhouWu");


  } else if (week == 6) {


    System . out . println ("ZhouLiu");


  } else  {


    System . out . println ("ZhouRi");


  }
  System . out . println ("Over") ;
}
}
分数(要有可推敲性)




import java . util . Scanner ;
public class HelloWorld  {
public static void main (String [ ]  args) {
  System . out . println ("Begin") ;
  Scanner sc = new Scanner (System . in ) ;
  System . out . println ("ShuRuYiGeFenShu") ;
  int score = sc . nextInt ( ) ;
  if (score >100|| score <0) {
  System . out . println ("error");
   } else if (score<=94 && score>=90) {
    System . out . println ("entaintment");
  } else if (score>=95 && score<=100) {
    System . out . println ("Bycile");
  } else if (score<=89 && score>=80) {
    System . out . println ("Bianxing");
  }  else  {
    System . out . println ("Bit");
  }
    System . out . println ("Over");
}
}
用swtich语句做周一到周日




import java . util . Scanner ;
public class HelloWorld  {
public static void main (String [ ]  args) {
  System . out . println ("Begin") ;
  Scanner sc = new Scanner (System . in ) ;
  System . out . println ("ShuRuYiGeXingQiShu") ;
  int week = sc . nextInt ( ) ;
  switch (week){
   case 1 :
    System . out . println("ZhouYi");
    break;
   case 2 :
    System . out . println("ZhouEr");
    break;
   case 3 :
    System . out . println("ZhouSan");
    break;
   case 4 :
    System . out . println("ZhouSi");
    break;
   case 5 :
    System . out . println("ZhouWu");
    break;
   case 6 :
    System . out . println("ZhouLiu");
    break;
   case 7 :
    System . out . println("ZhouRi");
    break;
   default :
    System . out . println("Error");
  }
    System . out . println("Over");
 
}
}
case穿透




import java . util . Scanner ;
public class HelloWorld  {
public static void main (String [ ]  args) {
  System . out . println ("Begin") ;
  Scanner sc = new Scanner (System . in ) ;
  System . out . println ("ShuRuYiGeXingQiShu") ;
  int number = sc . nextInt ( ) ;
  switch (number){
   case 3:
   case 4:
   case 5:
    System . out . println("Chun");
    break;
   case 6 :
   case 7:
   case 8:
    System . out . println("Xia");
    break;
   case 9 :
   case 10:
   case 11:
    System . out . println("Qiu");
    break;
   case 1 :
   cas
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-11-23 15:58, Processed in 0.133675 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表