概述
变量
字符
Boolean 值
反码,原码,补码
循序控制
分支控制
switch
多重循环
break
import java.util.Scanner;
public class Break01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum += i;
if (sum > 20) {
System.out.println(i);
break;
}
}
// int chance=3;
for (int i = 1; i <= 3; i++) {
//要想 一直循环输出,那么这个scanner一定要在循环体里面
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名称:");
String name = input.next();
System.out.println("请输入密码:");
int words = input.nextInt();
if ("丁真".equals(name)&&666==words) {
System.out.println("登录成功!");
break;
} else {
// chance--;
System.out.println("输入错误,你还有" +(3-i) + "次机会");
}
}
}
}
continue
return
练习
public class HomeWork01 {
public static void main(String[] args) {
int cash=100000;
int count=0;
while (true){
if (cash>50000){
cash*=0.95;
count++;
}else if (cash>=1000){
cash-=1000;
count++;
}else{
break;
}
}
System.out.println(count);
}
}
public class HomeWork02 {
public static void main(String[] args) {
// 水仙花数:153=1^3+5^3+3^3
int n=153;
int n1= n/100;//取百位,因为这里是int整数类型,所以n/100=1.53,实际上是1;
int n2=n % 100 /10;//取十位
int n3= n % 10;
if (n1*n1*n1+n2*n2*n2+n3*n3*n3==n){
System.out.println(n+"是水仙花数");
}else {
System.out.println(n+"不是水仙花数");
}
//打印a-z; A-Z
System.out.println("=============================");
for (char a='a';a<='z';a++){
System.out.print(a );
}
System.out.println();
for (char A='A';A<='Z';A++){
System.out.print(A);
}
}
}
数组
方法重载
可变参数
public class HspMethod01 {
public static void main(String[] args) {
// 如果返回类型是String 不是void
// Method no1 = new Method();
// 返回类型是字符串,所以这里直接输出也会是字符串形式
// System.out.println(no1.showScore("cao",12,23,43,4,6););
Method method = new Method();
method.showScore("cai",1,2,34);
}
}
class Method{
public static void showScore(String name,double...scores){
double sum=0;
for (int i=0;i<scores.length;i++){
sum+=scores[i];
}
System.out.println(name+"的成绩总分为:"+sum);
//return name+"成绩总分为:"+sum;//这里返回的事字符串
}
}
作用域
构造器