1. 输出输入
1.1输出
用System.out.println()来表示换行输出。
此外为了方便人来阅读,需要做一些格式化输出的处理。
1.1.1格式化输出
一般格式化输出使用System.out.printf(),通过使用占位符%、printf()来格式化后面的参数。
public class Main{
public static void main(String[] args){
double d = 3.1415926;
System.out.printf("%.2f\n",d);
System.out.printf("%.4f\n",d);
}
}
运行结果:
3.14
3.1416
除了格式化浮点数,还可以格式化其他数据类型:
占位符 | 说明 |
---|---|
%d | 格式化整数 |
%x | 格式化十六进制整数 |
%f | 格式化浮点数 |
%e | 格式化科学计数法表示的浮点数 |
%s | 格式化字符串 |
1.2输入
下面是一个栗子:
import java.util.Scanner;//导包
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);//创建Scanner对象
System.out.print("Input your name:");
String name = scanner.nextLine();
System.out.print("Input your age: "); // 打印提示
int age = scanner.nextInt(); // 读取一行输入并获取整数
System.out.printf("Hi, %s, you are %d\n", name, age); // 格式化输出
}
}
Java中的输入需要导入java.util.Scanner类之后再创建具体的对象。
创建Scanner对象并传入System.in,scanner会自动转换数据类。之后要读取用户输入的字符串,可以使用scanner.nextLine(),要读取整数,使用scanner.nextInt()。
2.if判断
一个栗子不赘述
public class Main{
public static void main(String[] args){
int n = 68;
if (n >= 90) {
System.out.println("优秀");
}
else if (n >= 60) {
System.out.println("及格了");
}
else{
System.out.println("挂了");
}
}
}
补充一些内容:
1.判断值类型的变量是否相等,用“==”运算符。
2.判断引用类型的变量内容是否相等,必须使用equals()方法:
if (s1.equals(s2)){
…
}
注意:执行语句s1.equals(s2)时,如果变量s1为null,会报NullPointerException,所以一般用&&来防止报错:
if (s1 != null && s1.equals(“hello”)) {
System.out.println(“hello”);
}
3.switch多重选择
1.一个例子
public class Main{
public static void main(String[] args){
int option=1;
switch(option){
case 1:
System.out.println("Selected 1");
break;
case 2:
System.out.println("Selected 2");
break;
case 3:
System.out.println("Selected 3");
break;
default:
System.out.println("Not selected");
break;
}
}
}
为避免没有选项时没有执行结果的情况,在最后加上default。
2.因为case语句具有穿透性,不写break会导致从当前选项往下全部执行,如下:
public class Main {
public static void main(String[] args) {
int option = 2;
switch (option) {
case 1:
System.out.println("Selected 1");
case 2:
System.out.println("Selected 2");
case 3:
System.out.println("Selected 3");
default:
System.out.println("Not selected");
}
}
}
运行结果:
Selected 2
Selected 3
Not selected
3.switch语句还可以匹配字符串来比较内容
String fruit = "apple";
switch (fruit) {
case "apple":
System.out.println("Selected apple");
break;
case "pear":
...
break;
case "mango":
...
break;
default:
...
break;
}
编译检查:eclipse中可以通过选择Preferences - Java - Compiler - Errors/Warnings - Potential programming problems,将以下检查标记为Warning:
‘switch’ is missing ‘default’ case
‘switch’ case fall-through
4.yield
一般switch内部会返回简单的值,但是需要复杂的语句的时候,就将语句放到{。。。}里,然后用yield返回一个值作为switch语句的返回值:
default -> {
int code = fruit.hashCode();
yield code; // switch语句返回值
}
4.while循环
4.1基本用法
while (条件表达式) {
循环语句
}
while循环是先判断循环条件,再循环,所以可能一次循环都不做。
另外还有一种情况:
public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1;
while (n > 0) {
sum = sum + n;
n ++;
}
System.out.println(n); // -2147483648
System.out.println(sum);
}
}
Java的int类型有最大值,达到最大值后,再加1会变成负数,结果,意外退出了while循环。
5.do while
先执行循环,再判断条件,条件满足时继续循环,条件不满足时退出,所以do while 至少执行一次循环。代码结构如下:
do {
执行循环语句
} while (条件表达式);
。
6.for循环
6.1用法
for (初始条件; 循环检测条件; 循环后更新计数器) {
// 执行语句
}
一个例子,对一个整型数组的所有元素求和
public class Main{
public static void main(String[] args){
int[] ns = {1,2,3,4,5};
int sum = 0;
for(int i = 0;i<ns.length;i++){
System.out.println("i = " + i+",ns[i]=" + ns[i]);
sum = sum + ns[i];
}
System.out.println("sum="+sum);
}
}
运行结果:
i = 0, ns[i] = 1
i = 1, ns[i] = 4
i = 2, ns[i] = 9
i = 3, ns[i] = 16
i = 4, ns[i] = 25
sum = 55
举个例子,水仙花
int count=0;
for (int i=100;i<1000;i++){
int a=i%10;
int b=i/10%10;
int c=i/10/10%10;
if (i==a*a*a+b*b*b+c*c*c){
System.out.println(i);
count++;
}
}
System.out.println(count);
for循环还可以缺少初始化语句、循环条件和每次循环更新语句,
6.2for each
for循环经常用来遍历数组,因为通过计数器可以根据索引来访问数组的每个元素:
int[] ns = { 1, 4, 9, 16, 25 };
for (int i=0; i<ns.length; i++) {
System.out.println(ns[i]);
}
7.break和continue
break和continue可以用于while和for循环。
7.1break
使用break语句跳出当前循环,即跳出自己所在的那一层循环:
public class Main {
public static void main(String[] args) {
for (int i=1; i<=3; i++) {
System.out.println("i = " + i);
for (int j=1; j<=3; j++) {
System.out.println("j = " + j);
if (j >= i) {
break;
}
}
// break跳到这里
System.out.println("breaked");
}
}
}
运行结果:
i = 1
j = 1
breaked
i = 2
j = 1
j = 2
breaked
i = 3
j = 1
j = 2
j = 3
breaked
7.2continue
continue则是提前结束本次循环,直接继续执行下次循环。break会结束整个循环。
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i=1; i<=5; i++) {
System.out.println("begin i = " + i);
if (i % 2 == 0) {
continue; // continue语句会结束本次循环
}
sum = sum + i;
System.out.println("end i = " + i);
}
System.out.println(sum);
}
}
运行结果:
begin i = 1
end i = 1
begin i = 2
begin i = 3
end i = 3
begin i = 4
begin i = 5
end i = 5
9
8.Random随机数
import java.util.Random; //导包
Random r= new Random();
int number=r.nextInt(10); //获取范围[0,10)