基础数据与运算
关键字和标识符
关键字:被赋予了特殊含义的字符,如class,public,void,if等等;
首先我们注意到这些关键字一般来说都是小写的,而且它们也是相当于一些特定的称谓,不能运用在其他的地方,或者不能去改变它们的含义。
标识符:这是我们自定义的一些名称,目的是为了增强我们代码的可读性,当然定义标识符的时候也存在组成规则的:
(1)由字母、数字、下换线、美元符组成;ps:数字不能作为开头使用;
(2)还有就是要严格的区分大小写,大小写不同表示的含义就会有所不同;
(3)不能使用关键字和Java自带的类名称作为标识符来使用;
注意:为了增强代码的可读性,应起有意义的单词作为其标识符。
标识符的组成规范(t规范与规则是有所不同的):
(1)小驼峰式命名法:如果一个标识符是由多个单词组成的一个词,那么除了第一个单词首字母小写外,其他单词首字母大写 ,如:myName等,主要应用于变量名、函数名;
(2)大驼峰式命名法:如果一个标识符是由多个单词组成的,那么所有单词的首字母大写,如:MyName,主要应用于类、接口;
(3)全大写命名法:如果一个标识符是由多个单词组成的,那么每一个单词都要大写,单词与单词之间用_连接;如MAX_VALUE DEFAULT_SIZE,主要应用于定义常量。
注释
注释:注释的存在也是为了更好的读懂代码,这其中有单行注释、多行注释、文档注释。但要注意单行可以套多行,多行可以套单行,多行不能套多行。
常量与变量
常量:固定的、不会变化的值;
分类:(1)整数:里边常见的又有二进制数、八进制数、十进制数、十六进制数;
(2)浮点数:如一般的小数3.14;
(3)字符:由一个数字、字母、符号被单引号包含的数据;
(4)字符串:由一个或多个数字、字母、符号被双引号包含的数据;
(5)布尔:有true和false两个组成;
(6)空:null 关键字
变量:本身可以改变的值
定义变量的格式 数据类型 变量名 = 初始化值;
变量的本质:变量本质就是在内存当中某一个进程里的一个临时存储区域;
注意:(1)每一个变量空间在创建的时候都会被随机的分配地址(真实的物理内存地址)
(2)变量空间中的数据在变化的过程中必须保持是可兼容的(至少是同一类型)
(3)如果直接在代码中用变量的物理地址去访问空间中的数据的话 有可能访问不到的。
接下来简单的看几个代码作业:
import java.util.Scanner;
public class Demo2_1{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a degree in Celsius:");
double C = scanner.nextDouble();
double Fahrenheit = (9.0 / 5) * C + 32;
System.out.println(C + " Celsius is " + Fahrenheit +" Fahrenheit");
}
}
import java.util.Scanner;
class Demo2_2{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.print("Enter the radius and length of a cylinder:");
double r=in.nextDouble();
double h=in.nextDouble();
double s=r*r*3.14159;
double v=h*s;
System.out.println("The area is "+s);
System.out.println("The volume is "+v);
}
}
import java.util.Scanner;
class Demo{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter a integer between 0 and 1000:");
int num=input.nextInt();
int sum=0;
sum=sum+num%10;
num=num/10;
sum=sum+num%10;
num=num/10;
sum=sum+num%10;
num=num/10;
System.out.println("The sum of the digits is"+sum);
}
}
import java.util.Scanner;
public class Demo2_4 {
public static void main(String[] args) {
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSeconds = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;
Scanner input = new Scanner(System.in);
System.out.print("Enter the time zone offset to GMT: ");
int time = input.nextInt();
input.close();
int hour = (int)currentHour + time;
System.out.println("The current time is " + hour + ":" + currentMinute + ":" + currentSeconds);
}
}
import java.util.Scanner;
public class Demo2_5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the monthly saving amount: ");
double amount = input.nextDouble();
double m = 0.05 / 12;
double January = amount * (1 + m);
double February = (100 + January) * (1 + m);
double March = (100 + February) * (1 + m);
double April = (100 + March) * (1 + m);
double May = (100 + April) * (1 + m);
double June = (100 + May) * (1 + m);
System.out.println(June);
}
}
import java.util.Scanner;
public class Demo2_6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter x1 and y1: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter x2 and y2: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.pow(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2), 0.5);
System.out.println("The distance between the points is " + distance);
}
}
import java.util.Scanner;
public class Demo2_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three points for a triangle: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
input.close();
double s1 = Math.pow(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2), 0.5);
double s2 = Math.pow(Math.pow((x2 - x3),2) + Math.pow((y2 - y3),2), 0.5);
double s3 = Math.pow(Math.pow((x3 - x1),2) + Math.pow((y3 - y1),2), 0.5);
double s = (s1 + s2 + s3) / 2;
double area = Math.pow(s * (s - s1) * (s - s2) * (s - s3), 0.5);
System.out.println("The area of the triangle is " + area);
}
}