public class lopp {
public static void main(String[] args) {
/*
判断偶数并输出
if 语法:
if(布尔){
语句;
}
*/
int a = 2;
if (a % 2 == 0) {
System.out.println("偶數");
}
}
}
public class lopp {
public static void main(String[] args) {
/*
if ...else 满足A输出A结果,不满足A输出B结果
if ...else 语法:
if(布尔){
语句1;
}else{
语句2;
}
*/
int bal = 500;
if (bal == 500) {
//System.out.println("有500w");
System.out.println("豪車");
}
else{
//System.out.println("没500w");
System.out.println("买自行车");
}
System.out.println("结束");
}
}
//if ...else判断奇偶
public class lopp {
public static void main(String[] args) {
int bal = 501;
if(bal % 2 ==0){
System.out.println("是偶数");
}else {
System.out.println("是奇数");
}
}
}
// if... else if
if(布尔){
语句;
}else if(布尔){
语句;
}else if(布尔){
语句;
}
public class lopp {
public static void main(String[] args) {
int bal = 3;
if(bal >= 50) {
System.out.println("bal="+bal);
}else if(bal >= 40 && bal < 50) {
System.out.println("bal="+bal+"在40-50之间");
}else if(bal >= 30 && bal < 40 ) {
System.out.println("bal="+bal+"在30-40之间");
}else if(bal < 30 ) {
System.out.println("bal="+bal+"小于30");
}
}
}
switch
switch(整数、枚举、字符串){
case常量1:语句1;break;// 无语句会执行下行语句
case常量2:语句2;break;
defaut:语句N;break;
}
public class lopp {
public static void main(String[] args) {
int bal = 6;
switch(bal) {
case 1: System.out.println("1");break;
case 2: System.out.println("2");break;
case 3: System.out.println("3");break;
default:System.out.println("无");break;
}
}
}
//字符串 1.7及以上版本支持
public class lopp {
public static void main(String[] args) {
String name = "痛苦代码";
switch(name) {
case "痛苦": System.out.println("痛苦找到");break;
case "痛苦代码": System.out.println("痛苦代码找到");break;
case "代码": System.out.println("代码找到");break;
default:System.out.println("无");break;
}
}
}
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World!");
//for循环 1-100累加
/*for(初始化表达式可赋值;布尔表达式;表达式循环后执行){
语句;
}*/
int sum = 0;
for(int i = 1 ; i <=100 ; i ++){
sum+=i;
System.out.println("sum"+sum);//放循环内打印每次的sum
System.out.println("i"+i);//放循环内打印每次的i
}
System.out.println("sum"+sum);//放循环外打印最终sum
}
}
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World!");
//while 1-100累加
/*
while(布尔表达式){
语句;
}
*/
int sum = 0;
int i = 1;
while(i <=100){
sum+=i;
i++;
System.out.println("i是"+i); //循环内打印每一个i
System.out.println("sum是"+sum);//循环内打印每一个sum
}
}
}
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World!");
//do...while 1-100累加
/*
do{
语句;
}
whlie(布尔表达式);
*/
int sum = 0;
int i = 1;
do{
sum+=i;
i++;
}while(i <=100);
System.out.println("sum"+sum); /结果5050
}
}
do...while执行语句前不考虑循环控制,while相反。
两次密码校验:
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String pwd1 = "0";
String pwd2 = "-1";
do {
System.out.println("请输入密码:");
pwd1 = sc.nextLine();
System.out.println("请再次输入密码:");
pwd2 = sc.nextLine();
if(!pwd1.equals(pwd2)) {
System.out.println("两次密码不一致");
}
}
while(!pwd1.equals(pwd2)) ;
System.out.println("密码设置成功");
}
}
遍历数组 for each 循环
int arr[] ={1,2,3,4,5,67,88};
for(int x :arr){
System.out.printLn("X");
}
public class lopp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cock,hen,chick;
for(cock = 1 ; cock<=20 ;cock++) {
for(hen = 1 ;hen<=33;hen++) {
for(chick = 3 ;chick <=99;chick+=3) {
if(cock*5+hen*3+chick/3 ==100) {//百钱
if(cock+hen+chick==100) { //百鸡
System.out.println(cock+"cock"+hen+"hen"+chick+"chick");
}
}
}
}
}