JAVA.day2

作者: 惨不忍睹 | 来源:发表于2016-07-11 19:54 被阅读16次

今天学的是java的基础知识,学到的内容非常多,还用了很多程序来体会这些知识,非常有收获。

关键词,标识符,注释  class 是类   public 说明是公有类     void 说明这个方法不需要返回任何值

写了一个数据转换程序

import java.util.Scanner;

public class WeightConverter {

/**

* 这是一个数字转换函数

* @param args

*/

public static void main(String[] args){

System.out.println("输入一个数");

Scanner sc = new Scanner(System.in);

double yuan = sc.nextDouble();

double xin = yuan * 0.5;

System.out.println("转换成的数为:"+xin);

sc.close();

}

}

变量,数据类型

写了一个两个名字交换的程序 用第三个中间变量,来实现转换

public class ChangeName {

//这是一个转换两个名字的程序

public static void main(String[] args){

String firstname = "周翔";

String secondname = "李超";

String thirdname;

System.out.println("firstname:"+firstname);

System.out.println("secondname:"+secondname);

System.out.println("*****************");

thirdname = firstname;

firstname = secondname;

secondname = thirdname;

System.out.println("firstname:"+firstname);

System.out.println("secondname:"+secondname);

}

}

创建一个空间,需要用new。数据类型    数字类型

byte  8位  -128to127      char  16位      short 16位    int  32位       long 64位(推荐使用)  float 32位     double 64位(推荐使用)

引用类型class(类)   interface(接口)   enum(枚举)   Array(数组)

写了一个对基础类型的数据做数据转换的程序

public class ChangeType {

public static void main(String[] args){

double a =32.2;

System.out.println(a);

int b = (int)a;

System.out.println(b);

}

}

布尔值(true false)    分支控制:if else;switch      循环控制:for  while

写了一个成绩等级查询的函数

public static void chengJi(int score)

{

if(score>90&&score<=100)

{

System.out.println("优秀");

}

else if(score>80&&score<=90)

{

System.out.println("良好");

}

else if(score>70&&score<=80)

{

System.out.println("中等");

}

else if(score>60&&score<=70)

{

System.out.println("合格");

}

else if(score>0&&score<60)

{

System.out.println("不合格");

}

else

{

System.out.println("输入错误");

}

}

写了一个根据学号查询名字的函数

public static void chaMingZi(int number)

{

switch(number)

{

case 101:System.out.println("a");break;

case 102:System.out.println("b");break;

case 103:System.out.println("c");break;

case 104:System.out.println("d");break;

case 105:System.out.println("e");break;

case 106:System.out.println("f");break;

case 107:System.out.println("g");break;

case 108:System.out.println("h");break;

default:System.out.println("don't know");break;

}

}

之后还介绍了强制转换 循环控制(for循环和while循环)用昨天的三五计数法来体会,数组的创建和赋值,还有字符串的使用

int a=65;

System.out.println(a);

char b=(char)a;

System.out.println(b);

这里是将int型转换为char型的程序

最后,做了一个今天的挑战,对二维数组进行赋值并打印,再做翻转后打印

public class TiaoZhan2{

public static void main(String[]args)

{

int a[][]=new int[5][5];

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

{

a[i][j]=10*i+j;

}

}

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

{

System.out.print(a[i][j]+" ");

}

System.out.println();

}

int b[][]=new int[5][5];

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

{

b[i][j]=a[j][i];

}

}

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

{

System.out.print(b[i][j]+" ");

}

System.out.println();

}

}

}

相关文章

  • JAVA.day2

    今天学的是java的基础知识,学到的内容非常多,还用了很多程序来体会这些知识,非常有收获。 关键词,标识符,注释 ...

网友评论

      本文标题:JAVA.day2

      本文链接:https://www.haomeiwen.com/subject/dozrjttx.html