1.java中的关键字
方法写在class里,语句写在方法里
package com.keke.day1;
public class Hello {
/*
public 访问权限符——谁都可以访问
static 可选的
void 没有返回值用void补位
main 方法名:小驼峰
小括号里面是参数详情,多个参数用逗号分割
做什么事(方法名),需要什么材料(小括号),输出什么(void)
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//不换行的输出 print 换行 println 输出文本后面换行
//快捷键:sout+ [alt+?]
System.out.println("111");
System.out.println("q");
System.out.print("111");
System.out.print("q");
}
}
1.数据类型
变量声明:变量类型 变量名=值
整型 byte int short long
范围不同 分别用1,2,4,8个字节存储 分为有符号和没符号的,如下是有符号的
byte -27~27-1
short -215~215-1
int -231~231-1
long -264~264-1
浮点型 float double 赋值时加f
精度不同 float小数点可以有7位,double可以有17位
布尔型 boolean
scanner
引入包
①ctrl+shift+O
②alt+/
2.水仙花数练习
package com.keke.day1;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
System.out.println("请输入一个三位数:");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
int one=(input/1)%10;
int ten=(input/10)%10;
int hun=(input/100)%10;
if(input==one*one*one+ten*ten*ten+hun*hun*hun){
System.out.println(input+"是水仙花数");
}
else{
System.out.println(input+"不是水仙花数");
}
}
}
2.测试方法junit——可以在一个类里写多个运行方法,方便测试
@Test注释——选住某个方法执行该方法,在空白处跑所有的测试都启动
package com.keke.day1;
import org.junit.Test;
public class practice_junit {
@Test
public void sub1(){
System.out.println("测试1");
}
@Test
public void sub2(){
System.out.println("测试2");
}
}
3.断点测试和i++/++i
先在要测试的语句左侧双击增加断点
然后选中该方法,右键debug as
按f6向下执行
按f8终止执行
y=i++ ①y=i ②i=i+1
y=++i ①i=i+1 ②y=i
package com.keke.day1;
import org.junit.Test;
public class demo02 {
@Test
public void try01(){
int i=250;
int y=i++;
System.out.println(y);
System.out.println(i);
}
@Test
public void try02(){
int i=250;
int y=++i;
System.out.println(y);
System.out.println(i);
}
}
4.数据类型转换
byte,short,char的运算都要先转换为int再计算;其中char变为对应的编码号进行运算。
@Test
public void try03(){
byte x1=12;
byte x2=11;
// 不支持用byte接收byte的运算,会让你转成int,除非强制转换byte类型
byte x3=(byte)(x1+x1);
System.out.println(x3);
}
@Test
public void try04(){
short y1=343;
short y2=12;
short y3=(short)(y1+y2);
System.out.println(y3);
}
@Test
public void try05(){
char z1='A';
char z2=32;
char z3=(char)(z1+z2);
System.out.println(z3);
}
5.练习
华氏摄氏互相转换
@Test
public void C_F(){
System.out.println("请输入摄氏温度:");
Scanner sc=new Scanner(System.in);
float c=sc.nextFloat();
float f=c*9/5+32;
System.out.println("摄氏温度"+c+"转换为华氏温度为"+f);
}
@Test
public void F_C(){
System.out.println("请输入华氏温度:");
Scanner sc=new Scanner(System.in);
float f=sc.nextFloat();
float c=(f-32)*5/9;
System.out.println("华氏温度"+f+"转换为摄氏温度为"+c);
}
大写转小写
@Test
public void ty10(){
char i='A';
char j=(char)(i+32);
System.out.println(j);
}
a,b值相互转换
@Test
public void try06(){
int a=1;
int b=2;
int c=a;
a=b;
b=c;
System.out.println("a:"+a);
System.out.println("b:"+b);
}
6.if else elif
import java.util.Scanner;
import org.junit.Test;
public class demo03 {
@Test
public void try1(){
// 输入成绩,输出等级
System.out.println("请输入成绩:");
Scanner sc=new Scanner(System.in);
int score=sc.nextInt();
if (score>90){
System.out.println("A");
}
else if (score>80){
System.out.println("B");
}
else if (score>70){
System.out.println("C");
}
else if (score>60){
System.out.println("D");
}
else {
System.out.println("E");
}
}
}
switch语句
@Test
public void try2(){
System.out.println("请输入成绩:");
Scanner sc=new Scanner(System.in);
int score=sc.nextInt()/10;
switch(score){
case 10:
case 9: #多个条件都是一个语句可以这样写,不写break就继续往下走
System.out.println("A");
break;
case 8:
System.out.println("B");
break;
case 7:
System.out.println("C");
break;
case 6:
System.out.println("D");
default:
System.out.println("E");
}
}