java基本数据类型
整数型:byte int long short
字符型:char
浮点型:double float
逻辑型:boolean
&&”和“||”
&&符号可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false。&&还具有短路的功能,即如果第一个表达式为false,则不再计算第二个表达式
||符号可以作逻辑或运算符,表示逻辑或(or),当运算符有一边为true时,整个运算结果为true!
if和switch
if用于区间值判断,适用于范围值。
switch是对具体指的判断,是固定的
switch中,没有固定语句break时,输出无限循环。case后只能接固定常量,不能接变量。当输入信息不匹配时,执行default语句。
用switch能做的,用if都能做,反之则不行。
while()加循环条件,一但条件不满足就不执行语句。
do-while(),判断条件满足则执行语句,条件不满足时退出循环,但是do-while()是先做再判断,所以至少要做一次循环。
如何注释代码
/**+回车
CTRL+/(选中要注释的代码)
判断是否是闰年
package com.lenovo.www; import java.util.Scanner; public class Year{ private static Scanner i; public static void main(String[] a){ i = new Scanner(System.in); System.out.println("输入年份"); int year=i.nextInt(); if(year/4==0&&year%100!=0||year/400==0){ System.out.println("闰年"); }else{ System.out.println("平年"); } } }
乘法口诀表
package com.lenovo.www; class ChengFaBiao { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int y = 1; y <= i; y++) { System.out.print(y + "*" + i + "=" + i * y + "\t"); } System.out.println(); } } }
成绩分类
class ChengFaBiao { public static void main(String[] args) { for(int x = 1; x <= 9; x++) { for(int y = 1; y <= x; y++) { System.out.print(y+"*"+x+"="+x*y+"\t"); } System.out.println(); } } }
季节判断
import java.io.*; public class Season { public static void main(String args[]) throws IOException { String month; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入月份:"); month = br.readLine(); String season; if (month.equals("12") || month.equals("1") || month.equals("2")) season = "冬天"; else if (month.equals("3") || month.equals("4") ||month.equals("5")) season = "春天"; else if (month.equals("6")|| month.equals("7") || month.equals("8")) season = "夏天"; else if (month.equals("9") || month.equals("10") ||month.equals("11")) season = "秋天"; else season = "不存在的月份"; System.out.println(month + "月份是" + season); } }
温度转换
import java.util.*; public class ex01 { public static void main(String[] args) { // TODO Auto-generated method stub double dh; double ds; System.out.println("请输入华氏度的值:"); Scanner sc = new Scanner(System.in); dh= sc.nextDouble(); ds= (dh-32)*5/9; System.out.println("转成摄氏度的值是:"+ds); } }
学生成绩分类
import java.util.Scanner; class student{ public static void main(String[] args){ Scanner in=new Scanner(System.in); String answer; do{ System.out.print("请分别输入学生的成绩:"); int score=in.nextInt(); System.out.println(); if(score<60&&score>=0){ System.out.println("不及格"); }else if(score<80){ System.out.println("合格"); }else if(score<90){ System.out.println("良好"); }else{ System.out.println("优秀"); } answer=nextInt(); }while(answer.equals("y")); System.out.println("程序结束"); } }