变量和数据类型

public class Bank {

/**

* 变量和数据类型的用法之int类型

*/

public static void main(String[] args) {

int money = 1000;

System.out.println("刘老师在银行的存款是:" + money);

}

}


基本数据类型的使用,double,String和char



public class Score {

/**

* @param args

*/

public static void main(String[] args) {

// 输出最高分同学的信息

double maxScore = 99.5;

String name = "姚远";// ^_^

// String sex="男";也可以

char sex = '男';

System.out.println("最高分同学的信息是:");

System.out.println("得分:" + maxScore);

System.out.println("姓名:" + name);

System.out.println("性别是:" + sex);

}

}


这个类的一部分演示了数学运算




double r;

Scanner s = new Scanner(System.in);

System.out.println("请输入半径:");

r = s.nextInt();

double area = r * r * 3.14;

System.out.println(area);



这是一个复杂一点的例子,计算加减乘除




import java.util.Scanner;

public class jisuan {

/**

* 输入两个数,计算它们的加减乘除的结果

*/

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("请输入两个整数:");

int zs1 = input.nextInt();

int zs2 = input.nextInt();

int j = zs1 + zs2;

int jian = zs1 - zs2;

int c = zs1 * zs2;

double chu = (double) zs1 / zs2;

System.out.println("这两个数字加减乘除的结果分别是:" + j + "\n" + jian + "\n" + c

+ "\n" + chu);

}

}



public class Operator {

/**

* 运算符的使用,演示了++和%的用法,注意,部分语言不支持浮点类型的余运算

*/

public static void main(String[] args) {

int a = 10, b = 6;

a++;

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

double d = 11.7;

d++;

System.out.println(d);

int c = a / b;

System.out.println(c);// 为什么是1

int y = a % b;

System.out.println(a + "%" + b + "=" + y);

double d2 = 5.3;

double d3 = d % d2;

System.out.println(d3);

}

}

public class Score {

/**

* @param args

*/

public static void main(String[] args) {

// 输出最高分同学的信息

double maxScore = 99.5;

String name = "姚远";// ^_^

// String sex="男";也可以

char sex = '男';

System.out.println("最高分同学的信息是:");

System.out.println("得分:" + maxScore);

System.out.println("姓名:" + name);

System.out.println("性别是:" + sex);

}

}


如下的例子演示了基本数据类型的赋值:直接拷贝





public class Score1 {

public static void main(String[] args) {

int zhanghaoScore = 80;

int wangScore = zhanghaoScore;

zhanghaoScore = 91;

System.out.println("王蒙的成绩是:" + wangScore);

}

}






char其实是数字

public class Zhuanhuan {

/**

* 演示了char,其实是数字

*/

public static void main(String[] args) {

char s = '刘';

int i = s;

int j = '张';

System.out.println(i);

System.out.println(j);

}

}




数据类型的转换,损失的不光是小数点

public class Zhuanhuang2 {

/**

*

* 如果一个数字没有小数点,其实他是int,想变成double,后面加上字母d

*/

public static void main(String[] args) {

double a = 9.8;

double b = 7.6;

int now = (int) (a + b);// 类型强转

System.out.println(now);

double c = 393993339393939393d;

int i = (int) c;

System.out.println(i);

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔...
    叶总韩阅读 5,166评论 0 41
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,916评论 0 2
  • 第一章 初识javaJAVA 第一讲:什么是程序?:为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的...
    人子日月几点阅读 544评论 0 1
  • 1 顺序语句 语句:使用分号分隔的代码称作为一个语句。 注意:没有写任何代码只是一个分号的时候,也是一条语句,...
    哈哈哎呦喂阅读 408评论 0 0
  • 前言 真的,一定会有一些小伙伴直到完整了几个项目都做完了,却从未用过渲染函数和JSX,所以一旦遇到需要复杂处理的模...
    microkof阅读 341评论 0 1