3.1 eclipse项目导入导出
-
eclipse新建项目
-
关闭项目
-
导出项目
-
项目导入
3.2 java程序输入方式
Scanner s = new Scanner(System.in);
long n = s.nextLong();
System.out.println(n);
Console c = System.console();
System.out.print("请输入账号:");
String a = c.readLine();
System.out.print("请输入密码:");
String p = new String(c.readPassword());
System.out.printf("您的账号是:%s,密码:%s。 \n", a, p);
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
if (a % b == 0) {
System.out.printf("%d ÷ %d = %d \n", a, b, a / b);
} else {
System.out.printf("%d ÷ %d = %d …… %d \n", a, b, a / b, a % b);
}
int sum = 0;
for (String s : args) {
sum += Integer.parseInt(s);
}
System.out.println(sum);
String name = JOptionPane.showInputDialog(null, "请输入姓名", "财务软件提示", JOptionPane.INFORMATION_MESSAGE);
System.out.println(name);
int age = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入年龄", 18));
System.out.println(age);
3.3 java格式化输出
package cn.webrx;
import java.util.Date;
import javax.swing.JOptionPane;
public class Out1 {
public static void main(String[] args) {
System.out.println();
System.out.println("hello");
double i = 10;
//java.lang 包所有对象,不需要导入import 可以直接使用
System.out.print(Math.pow(2, 8));
var d = new Date();
System.err.println("\nerror");
JOptionPane.showMessageDialog(null, "大家好", "系统提示", JOptionPane.QUESTION_MESSAGE);
}
}
输出内容到文件中
package cn.webrx;
import java.io.FileWriter;
import java.io.IOException;
public class Out2 {
public static void main(String[] args) throws IOException {
String str = "hello world 中国人";
FileWriter fw = new FileWriter("user.txt");
fw.write(str);
fw.close();
}
}
控制台格式化输出方式
package cn.webrx;
import java.util.Date;
public class Out3 {
public static void main(String[] args) {
System.out.printf("%d %02d %.2f %2$06d \n", 2, 5, 1.98245);
System.out.println(System.currentTimeMillis());
System.out.println(new Date().getTime());
System.out.printf("%n%tF %1$tT%n",System.currentTimeMillis());
}
}
3.3.1 格式化字符串
package cn.webrx;
public class StrFormat {
public static void main(String[] args) {
String n = "james";
System.out.printf("java之父:%s 。\n", n);
int age = 22;
String info = String.format("他的姓名是:%s,年龄:%02d岁。", n, age);
System.out.println(info);
//字符串转义符 java字符串,是对象String是双引号 java15 可以有三个双引号,多行字符串
//\t \n \r\n \' \" \\
//java15 最新的字符串,多行字符串功能
String sss = """
a\ns\tfasfasf
asdf
as
\'f\'
asd
"f"
int i = 60;
safasfd
asf
asf
""";
System.out.println(sss);
//hello "java".
System.out.println("h\\ello \"java\".");
System.out.println('h');
System.out.println("\'hello\'");
}
}
3.3.2 格式化日期时间对象
package cn.webrx;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateForm {
public static void main(String[] args) throws ParseException {
var d1 = new Date();
var c1 = Calendar.getInstance();
var ll = System.currentTimeMillis();
System.out.printf("%1$tY年%1$tm月%1$td日%n", d1);
System.out.println(String.format("%1$tH:%1$tM:%1$tS", d1));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d1));
System.out.println(sdf.format(c1.getTime()));
SimpleDateFormat s2 = new SimpleDateFormat("yyyyMMddHHmmssS");
System.out.println(s2.format(ll) + ".jpg");
SimpleDateFormat s3 = new SimpleDateFormat("MM月dd日");
System.out.println(s3.format(System.currentTimeMillis()));
// 将一个字符串转换日期对象
String ss = "1985-10-20";
SimpleDateFormat s4 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(s4.parse(ss)));
// 计算某日期是星期几 2020-10-1 请问这一天是星期几
SimpleDateFormat s5 = new SimpleDateFormat("u");
System.out.println(s5.format(s4.parse("2020-10-1")));
// 计算两个日期之间的天数
SimpleDateFormat s6 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss1 = "2020-1-1 0:0:0";
String ss2 = "2021-1-1 0:0:0";
long ms = s6.parse(ss2).getTime() - s6.parse(ss1).getTime();
System.out.printf("%n %s 到 %s 共 %d 天。", ss1, ss2, ms / 1000 / 60 / 60 / 24);
}
}
3.3.3 格式化数字
NumberFormat
- 使用getInstance或getNumberInstance获取正常的数字格式。
- 使用getIntegerInstance得到的整数格式。
- 使用getCurrencyInstance来获取货币数字格式。
- 使用getPercentInstance获取显示百分比的格式。
package cn.webrx;
import java.text.NumberFormat;
import java.util.Locale;
public class NumForm {
public static void main(String[] args) {
int n1 = 9824412;
System.out.println(n1);
System.out.println(String.format("%.2f", n1 * 1.0));
// 格式化百分比
NumberFormat nf = NumberFormat.getPercentInstance();
System.out.println(nf.format(.25));// 25%
NumberFormat nf2 = NumberFormat.getInstance();
System.out.println(nf2.format(n1));// 9,824,412
System.out.println(nf2.format(100.325825));// 100.326
// Locale.setDefault(Locale.CHINA);
NumberFormat nf3 = NumberFormat.getCurrencyInstance();
System.out.println(nf3.format(1000.325825));
}
}
3.4 分支语句if switch
3.4.1 if
if() 语句;
if(){
语句1;
语句2;
语句3;
}
if(){
}else{
}
if(){
}else if(){
}else if(){
}else{
}
If1.java
package cn.webrx;
import java.util.Scanner;
public class If1 {
public static void main(String[] args) {
if (true)
System.out.println("true1");
System.out.println("true2");
System.out.println("true3");
if (1 == 1) {
System.out.println("1==1");
}
Scanner sc = new Scanner(System.in);
System.out.print("请输入年龄:");
int age = sc.nextInt();
if (age >= 18) {
System.out.println("欢迎");
} else {
System.out.println("禁止访问");
}
}
}
If2.java
package cn.webrx;
import java.util.Calendar;
public class If2 {
public static void main(String[] args) {
var c = Calendar.getInstance();
var k = c.get(Calendar.DAY_OF_WEEK);
// k 1-7 星期日 星期一 星期六
if (k == 1)
System.out.println("星期日");
if (k == 2)
System.out.println("星期一");
if (k == 3)
System.out.println("星期二");
if (k == 4)
System.out.println("星期三");
if (k == 5)
System.out.println("星期四");
if (k == 6)
System.out.println("星期五");
if (k == 7)
System.out.println("星期六");
System.out.println();
System.out.println("今日是:星期" + "日一二三四五六".charAt(k - 1));
System.out.println();
if (k == 1) {
System.out.println("星期日");
} else if (k == 2) {
System.out.println("星期一");
} else if (k == 3) {
System.out.println("星期二");
} else if (k == 4) {
System.out.println("星期三");
} else if (k == 5) {
System.out.println("星期四");
} else if (k == 6) {
System.out.println("星期五");
} else {
System.out.println("星期六");
}
}
}
案例:判断输入的内容是中文、还是英文、还是数字
package cn.webrx;
import java.util.Scanner;
public class If3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入词汇:");
String w = sc.nextLine();
if (w.matches("^\\d+$")) {
System.out.println("你输入的是纯数字");
} else if (w.matches("^[a-zA-Z ]+$")) {
System.out.println("你输入的是纯英文");
} else if (w.matches("^[\u4e00-\u9fa5]+$")) {
System.out.println("你输入的是汉字");
} else {
System.out.println("输入有误");
}
sc.close();
}
}
3.4.2 随机实现
//java.util.Random
//java.lang.Math.random()
package cn.webrx;
import java.util.Random;
public class Rand1 {
public static void main(String[] args) {
//随机小数 0-1 范围
System.out.println(Math.random());//double
Random rand = new Random();
System.out.println(rand.nextFloat());//float
System.out.println(rand.nextDouble());//double
System.out.println(rand.nextInt());
System.out.println(rand.nextInt(3));//0-2 3个整数
System.out.println(rand.nextLong());
System.out.println(rand.nextBoolean());
if(rand.nextBoolean()) {
System.out.println("张三");
}else {
System.out.println("李四");
}
}
}
package cn.webrx;
import java.util.UUID;
public class Rand2 {
public static void main(String[] args) {
//GUID UUID
UUID id = UUID.randomUUID();
System.out.println(id);
System.out.println(id.toString().toLowerCase()+".jpg");
System.out.println(id.toString().toUpperCase());
}
}
案例:猜数游戏
package cn.webrx;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Rand4 {
public static void main(String[] args) throws IOException {
// 猜数游戏
Random rand = new Random();
// 生成1-100 之间的随机整数
int num = rand.nextInt(1000) + 1;
Scanner sc = new Scanner(System.in);
int index = 0;
while (true) {
++index;
System.out.print("请输入数字:");
int n = sc.nextInt();
if (n > num) {
System.out.printf("%d、太大了%n", index);
} else if (n < num) {
System.out.printf("%d、太小了%n", index);
} else {
if (index == 1) {
System.out.printf("大神,恭喜您,猜对了,你的游戏成绩(100分)%n");
} else {
System.out.printf("%d、恭喜您,猜对了,你的游戏成绩(%d 分)%n", index, 100 - index * 10);
}
break;
}
}
}
}
日期操作 随机操作
作业:
1、实现生成指定范围的随机日期
2、编写程序实现随机生成双色球效果
3.4.3 switch
var c = Calendar.getInstance();
int k = c.get(Calendar.DAY_OF_WEEK);
System.out.println(k);
switch (k) {
case 1:
System.out.println("星期日");
break;
case 2:
System.out.println("星期一");
break;
case 3:
System.out.println("星期二");
break;
case 4:
System.out.println("星期三");
break;
case 5:
System.out.println("星期四");
break;
case 6:
System.out.println("星期五");
break;
default:
System.out.println("星期六");
break;
}
面试题:java switch语句支持哪些类型?
package cn.webrx;
public class SwitchTest2 {
public static void main(String[] args) {
int i = 3;
short a = 4;
byte b = 100;
// java switch支持的类型: byte short int char enum 枚举
long dd = 100L;
char c = 'a';
boolean f = true;
float fl = 1.0f;
double du = 2.0d;
String name = "lisi";
User level = User.STUDENT;
switch (level) {
case ADMIN:
break;
case USER:
break;
case STUDENT:
break;
case TEACHER:
break;
default:
break;
}
}
}
enum User {
ADMIN, MEMBER, USER, STUDENT, TEACHER, MARSTER
}
package cn.webrx;
import java.time.LocalDate;
public class SwitchTest3 {
public static void main(String[] args) {
// 输入年月,输出这一个有多少天
int year = 2020;
int month = 12;
int days = 31;
switch (month) {
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
LocalDate now = LocalDate.of(year, 1, 1);
days = now.isLeapYear() ? 29 : 28;
break;
}
System.out.printf("%d年%d月有(%d)天 \n", year, month, days);
}
}
package cn.webrx;
public class SwitchTest4 {
public static void main(String[] args) {
// 输入年月,输出这一个有多少天
int year = 2012;
int month = 2;
// java 13 开始支持表达式
int days = switch (month) {
case 1, 3, 5, 7, 8, 10, 12 -> 31;
case 4, 6, 9, 11 -> 30;
default -> year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28;
};
System.out.printf("%d年%d月有(%d)天 \n", year, month, days);
//
switch (month) {
case 1, 3, 5, 7, 8, 10, 12 -> {
System.out.println("*".repeat(31));
}
case 4, 6, 9, 11 -> {
System.out.println("*".repeat(30));
}
default -> {
int n = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28;
System.out.println("*".repeat(n));
}
}
}
}
if else 三元运算 switch 总结
package cn.webrx;
public class If4 {
public static void main(String[] args) {
// 三元运算符 ? :
//
int i = true ? 11 * 11 : 12 * 12;
System.out.println(i);
System.out.println("123".equals("123") ? "yes" : "no");
//byte short int char enum String 表达式
int n = 10;
switch (n) {
case 1, 2, 4, 10, 6, 3:
System.out.println("ok");
break;
case 11,22,33,44:
System.out.println("nnnn");
break;
}
n = 3;
switch (n) {
case 1, 2, 4, 10, 6, 3 -> {
System.out.println("ok");
}
case 11, 22, 33, 44 -> {
System.out.println("nnnnn");
}
}
n = 3;
String sss = switch (n) {
case 1, 2, 4, 10, 6, 3 -> "yes";
case 11, 22, 33, 44 ->"no";
default->{
int a = 5;
yield "ok";
}
};
System.out.println(sss);
}
}
作业:编写程序实现输入指定的年月的那个月共有多少天?
3.5 循环语句for while
-
程序流程结构
- 顺序结构
- 分支结构 if else if else switch case default ? : (三元运算符)
- 循环结构
-
循环结构
-
for 计数型循环语句 for break终止循环跳出循环 continue继续结束一次循环,继续下一次循环。
for(循环变量初始化;条件;增量){ //编写循环体语句代码 } for(;;); for(;;){ } for(;true;){ } package cn.webrx; public class For2 { public static void main(String[] args) { // 如下代码是求:1+2+3+...+100 = ? int s = 0; for (int i = 1; i <= 100; i++) s += i; System.out.println(s); // 求1..100 素数的和 int num = 37; boolean flag = true; for (int i = 2; i <= num - 1; i++) { if (num % i == 0) { flag = false; break; } } if (flag) { System.out.printf("%d 是素数。 \n", num); } else { System.out.printf("%d 不是素数。 \n", num); } } } //输出结果7 9 11 for (int i = 6; i <= 15; i++) { if (i % 2 == 0) continue; //结束一次循环,立即开始下一次 System.out.printf("%d \n", i); if (i > 10) break;//终止循环 }
-
for each(for in)
for(int i : 数组 或 集合){} // 声明数组 int[] ns = { 10, 20, 30, 40, 50 }; System.out.println(ns.length);// 输出数组元素的个数 5 // 将数组元素全部显示出来 for (int i = 0; i < ns.length; i++) { System.out.printf("ns[%d] = %d \n", i, ns[i]); } //for each (for in) for(int t : ns) { System.out.println(t); } package cn.webrx; import java.util.Collection; public class For7 { public static void main(String[] args) { //显示当前java系统所有系统信息 Collection ss = System.getProperties().values(); for(Object o : ss) { System.out.println(o); } } }
-
while 条件循环语句(do while())
package cn.webrx; public class While1 { public static void main(String[] args) { while(true) { System.out.println("不是死循环"); break; } //12345 while(条件){} int i = 1; while(i<=-5) { System.out.println(i++); } } } package cn.webrx; public class While1 { public static void main(String[] args) { while(true) { System.out.println("不是死循环"); break; } //12345 while(条件){} int i = 1; while(i<=-5) { System.out.println(i++); } } }
-
总结:if else if else switch case default java13 switch 支持表达式返回值 yield
for for in
while do while
break continue
3.6 作业
(1)、求1..100 素数的和
(2)、打印输出九九乘法表
(3)、打印输出菱形图案效果
(4)、在123456789,任意地方放加减,要求表达式计算结果等于100。 (11个)
package cn.exam;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ex1 {
public static void main(String[] args) {
Random rand = new Random();
String[] strs = { "", "+", "-" };
Pattern p = Pattern.compile("-?[0-9]+");
Set<String> set = new HashSet<String>(); //Set集合 ,
long start = System.currentTimeMillis();
int mn = 0;
while (true) {
++mn;
String str = "1";
for (int i = 2; i <= 9; i++) {
str += strs[rand.nextInt(3)] + i;
}
Matcher m = p.matcher(str);
int sum = 0;
while (m.find())
sum += Integer.parseInt(m.group());
if (sum == 100)
set.add(str);
if (set.size() == 11)
break;
}
long end = System.currentTimeMillis();
for (String st : set)
System.out.println(st + "= 100");
System.out.printf("用时:%dms,共循环%d次",(end-start),mn);
}
}
1+23-4+5+6+78-9= 100
123+4-5+67-89= 100
1+23-4+56+7+8+9= 100
12+3-4+5+67+8+9= 100
123-45-67+89= 100
12-3-4+5-6+7+89= 100
123-4-5-6-7+8-9= 100
1+2+34-5+67-8+9= 100
123+45-67+8-9= 100
12+3+4+5-6-7+89= 100
1+2+3-4+5+6+78+9= 100
用时:178ms,共循环16847次
(5)、利用随机、while循环、Scanner键盘输入,实现猜数游戏
(6)、1~100之间不能被3整除的数之和