day02_变量数据类型和运算符

public class jh_07_表达式的概念 {
 public static void main(String[] args) {
 
  /*
   * 是由符号和操作数构成的序列。
   * 10
   * a;
   * a = 10;
   * a + b * 10
   * 可以把一个表达式的内容赋值给一个变量。
   * sum = a + b
   * x = 10
   * y = 2 * x + 1
   */
  
   }
}


public class jh_o8_变量间的赋值交换两个数值 {
 public static void main ( String [] arge ) {
  //交换两个变量的值。
  
  /*
   *左手和右手互换。
   * 思路:
   * 1:把其中一个手(左)的东西放桌子上;
   * 2:把另外一只手(右)的东西给左手。
   * 3:把桌子上的东西放给右手;
   * ------
   * 左手--------桌子
   * 右手--------左手;
   * 桌子--------右手;
   */
  
  int a = 10; // 左;
  int b = 8; // 右;
  
  //声明一个变量表示桌子;
  int temp ;
  // 左手-------桌子;
//  桌子 = 左手
  temp= a;
//      右手-------左手
  a = b;
//      桌子-------右手;
  b = temp;
  
  System.out.println(a);
  System.out.println(b);
  
  
 }
 
  
}


public class jh_09_算数运算符 {
 public static void main(String[] args) {
  /*
   * 加
   * 减
   * 乘
   * 除
   * 取余(或者叫取模)
   */
  
  
  int a = 7;
  int b = 4;
  int sum = a + b;
  System.out.println(1000);
  int money = 1000;
  System.out.println(money);
    
  System.out.println(sum);
  
  
  System.out.println(a + b);// 加
  System.out.println(a - b);// 减
  System.out.println(a * b);// 乘
  // 整数除整数还是整数。
  System.out.println(a / b);// 除
  
  
  // % 取余 --左边和右边做除法,把余数返回。
  System.out.println(a & b);
  
  
//        从控制台输入学员王浩3门课程成绩
  int stb = 99,java = 88,sql = 76;
  
  int cha = java - sql;
  double avq = (stb + java + sql)/3.0;
  
  
  System.out.println("----------");
  System.out.println("STB\tJAVA\tSQL");
  System.out.println(stb + "\t" + java+"\t"+sql);
  System.out.println("----------");
  System.out.println("成绩差:"+cha);
  System.out.println("平均分:"+avg);
 }
 
 
 
 
}


public class jh_10_复合赋值运算 {
 public static void main(String[] args) {
  //复合赋值运算符 = + - * / %
  
  int a = 5,b = 2;
  System.out.println(a);
  
  a += b;//左边和右边做加法,然后赋值给左边
  // a = a + b
  
  System.out .println(a);
  
  a -= b;
  a *= b;
  a /= b;
  a %= b;
  /*
   *  += 后面用于累加求和
   *  1 + 2 + 3 + 4 + 5
   *  (1 + 2) + 3
   *  sum + 3
   *  sum = sum + 3;
   *  sum += 3;
   *  ((1 + 2) + 3) + 4
   *  sum + 4;
   *  sum = sum + 4;
   *  sum += 4;
   * 
   *  i = 1
   *  sum = 0;
   * 
   *  sum += i;
   *  print(sum)
   *  i = 2;
   *  sum += i;
   *  -- sum = 3
   *  i = 3;
   *  sum += i;
   * 
   */
  
  
 }
 
}
 

public class jh_11_加加减减运算符 {
 public static void main(String[] args) {
  
  /*加加与减减的对比
   * ++:自加。对原有数据进行加1
   * --:自减。对原有数据进行减1
   */
  
  /*单独使用对比参与运算使用
   * 1:单独使用:放在操作数的后面和前面的效果一样。(常见)
   * 2:参与运算使用:
          *放在操作数的前面,先自增自减然后再参与运算使用。
             *放在操作数的后面,先参与运算,再自增自减。
   */
  
  
  int a = 5;
  System.out.println(a);//5
  int b = 2;
  System.out.println(b);//2
  System.out.println("=========");
  
  
  //System.out.println( a ++);
  // a ++  对自身加1
  //System.out.println( b --);
  // b --  对自身减1
  
  int count = 0;
  
  //   count ++;
     //System.out.println(count);
  
  
  //   count --;
  //System.out.println(count);
  
  
 }
}
 

public class jh_12_a加加与加加a的区别 {
 public static void main(String[] args) {
  int a = 5;
  int b = 5;
  
  int x = a ++ *2;
  System.out.println(x);
  int y = b* 2;
  System.out.println(y);
  /*
   *  a ++ 是先做运算然后再对自身加1
   *  ++ a 是先自己加1,然后再做运算
   */
  System.out.println(a);
  System.out.println(b);
  
  
  
  //System.out.println( a ++ );// 5
  // a ++ 先输出然后再加1
  //System.out.println( ++ b);
  // ++ b 先对自己加1然后再输出。
  
  
//       a ++ ;
//   ++ b;
//  
//   不管在哪都是对自己加1
//       System.out.println(a);
//   System.out.println(b);
  
  
 }
}
 

import java.util.Scanner;
public class jh_13_键盘录入 {
 /*
  * 1 :  代码块 : { }----大括号括起来的内容
  * 2 : 函数 : 完成特定功能的代码块
  * nextInt();
  * 3:使用,调用函数。对象名.函数名();
  * 4: 键盘录入 Scanner 的对象。
  * Scanner sc = new Scanner(System.in) ;
  * 对象名.函数名();
  * sc.nextInt();
  * int a = sc.nextInt();
  *
  */
 
 public static void main(String[] args) {
  // 创建键盘录入对象
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入a的值:");
  //在控制台输入一个整数,
  //把输入的这个整数赋值给a。
  int b = 10;
  int a = sc.nextInt();
  System.out.println(a);
  
 }
}
 

public class jh_14_自动类型转换举例 {
 /*某学生第一次分数为81.29,
  * 第二次比第一次多2分,
  * 求第二次的分数?
  */
 public static void main(String[] args) {
  //第一次分数81.29
  double firstScore = 81.29;
  //第二次分数比第一次多两分
  double secondScore ;
  secondScore = firstScore + 2;
  
  /*
   * 2---- 2.0
   * 2.0----- 2
   */
  //输出结果:
  System.out.println("第二次的分数是: "+ secondScore);
     double a = 2;
     System.out.println(a);
     //自动类型转换   隐式转换
     //强制 ---- ---- ----
    
 }
}
public class jh_15_强制类型转换 {
 public static void main(String[] args) {
  //去年Apple笔记本所占市场份额是20,
  int lastYear = 20;
  
  //今年增长的市场份额是9.8
  double thisYear = lastYear + 9.8;
  //可转换为
 // int thisYear = (int) thisYear + 9.8;
  
 // (int) (lastYear + 9.8)
 //         lastYear + 9.8
 // 类。类型。 
  int thisYear = lastYear + (int) 9.8;
  
  
 }
 
}
 
 
 
 
 
 
 
 

 
 
public class jh_18_如何使用Boolean类型 {
  /*从控制台输入张三同学的成绩,
   * 与李四的成绩(80分)比较,
   * 输出“张三的成绩比李四的成绩高吗?”
   *  的判断结果
   */
 public static void main(String[] args) {
  // Scanner sc = new Scanner(System.in);
//     从控制台输入张三分数
  int a = 10;
  System.out.println(a);
  System.out.println("请输入张三的成绩:");
  int zhangScore = 80;
  
//      与李四的成绩(80)分比较
//      李四的成绩(80)
  int liScore = 80;
  
//      张三的成绩比李四的成绩高吗?
  boolean result = zhangScore > liScore;
  
  //输出结果
  System.out.println("高吗?"+result);
 }
}
 

 
public class jh_20_学院操作_打印小票 {
  /*
   * 各商品的消费金额之和 * 折扣
   */
  public static void main(String[] args) {
  // 商品名称
      String tx = "T恤";
      String wqx = "网球鞋";
      String wqp = "网球拍";
  
     // 单价
      int txPrice = 245;
      int wqxPrice = 570;
      int wqpPrice = 320;
     
     // 个数
      int txNum = 2;
      int wqxNum = 1;
      int wqpNum = 1;
     
    // 计算各个商品的金额;
      int txMoney = txPrice * txNum;
      int wqxMoney = wqxPrice * wqxNum;
      int wqpMoney = wqpPrice * wqpNum;
  
    //输出截图上半部分。
  System.out .println("*************消费单*************");
  System.out .println("购买物品\t单价\t个数\t金额");
  System.out .println(tx + "\t¥"+txPrice+"\t"+txNum+"\t¥"+txMoney);
  System.out .println(wqx + "\t¥"+wqxPrice+"\t"+wqxNum+"\t¥"+wqxMoney);
  System.out .println(wqp + "\t¥"+wqpPrice+"\t"+wqpNum+"\t¥"+wqpMoney);
 
    //给出折扣
  double discount = 0.8; // 8折
  
    //计算消费总金额----allmoney:
  double allmoney = (txMoney+wqxMoney+wqpMoney)*discount;
  
  //给出实际交费金额 1500
  int money = 1500;
  
  //计算找钱。leftMoney ;
  double leftMoney = money - allmoney;
  
  // 计算积分。 100积3分.
  int integral = (int) (allmoney / 100*3);
  
  //按截图内容 输出结果
  
  System.out.println("折扣\t"+(int)(discount*10)+"折");
  System.out.println("消费总金额:  ¥"+allmoney);
  System.out.println("找钱:\t"+leftMoney);
  System.out.println("积分:\t"+integral);
  
  /*
   * 1:给出九个变量名称。储存内容。
   * 2:输出截图上半部分
   * 3:做运算。
   * 4:输出运算后的结果。
   */
  
  
  
  
  }
}
 

 
 
 

猜你喜欢

转载自www.cnblogs.com/sirendingzhi/p/11121084.html