Java学习笔记-后端论坛-技术分享-千百度社区

Java学习笔记

Java学习笔记

自增自减运算符

短路逻辑运算符

两只老虎,用三元运算符比较大小

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

最后编辑于 : 2022.07.04 16:39:13 © 著作权归作者所有,转载或内容合作请联系作者

请登录后发表评论

    没有回复内容