p110-p154
控制结构(第四章)
多分支
if-elseif-else
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner myscanner = new Scanner(System.in);
System.out.println("input your score?");
int score = myscanner.nextInt();
if(score==100){
System.out.println("excellent!!");
}
else if (score<=99&&score > 80){
System.out.println("GOOD~~");
}
else if (score<=80&&score>=60){
System.out.println("ok");
}
else{
System.out.println("not ok!!");
}
System.out.println("go on...your score is\t"+score);
}
}
但是你输入110的时候也会报不及格哎!我怎么没想到...所以我们先对输入的信用分进行有效判断
如果用户输入的是hello咋整捏?好吧听说后面会讲...(异常处理)
案例:
输出b啊
嵌套分支
建议不要超过3层。
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner myscanner = new Scanner(System.in);
System.out.println("input your score?");
double score = myscanner.nextDouble();
System.out.println("input your gender?(man/woman)");
String gender = myscanner.next();
if(score>8.0){
if (gender.equals("man")){
System.out.println("you will go to man's final competition");
}
else if(gender.equals("woman")){
System.out.println("you will go to woman's final competition");
}
}
else{
System.out.println("youe are fired!");
}
System.out.println("go on...your score is\t"+score);
}
}
练习:
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner myscanner = new Scanner(System.in);
System.out.println("input your age?");
int age = myscanner.nextInt();
System.out.println("input the month(like 1,2,3,4...)");
int month = myscanner.nextInt();
if(month>=4&&month<=10){
System.out.println("it's wangji");
if (age<18&&age>0){
System.out.println("you are kid,give me 30 yuan");
}
else if(age>=18&&age<60){
System.out.println("you are adult, give me 60 yuan");
}
else if(age>=60){
System.out.println("you are elder, give me 20 yuan");
}
else {
System.out.println("your age is a problem");
}
}
else if ((month<4&&month>=1)||(month>10&&month<=12)){
System.out.println("it's danji");
if(age>=18&&age<60){
System.out.println("you are adult, give me 40 yuan");
}
else if(age>=0&&age<18||age>=60){
System.out.println("your are not adult, give me 20 yuan");
}
}
else{
System.out.println("your month is a problem");
}
System.out.println("go on...");
}
}
switch分支
学过了,敲下练习吧。
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner myscanner = new Scanner(System.in);
System.out.println("input a/b/c/d/e/f/g?");
char c = myscanner.next().charAt(0);
switch(c){
case('a'):{
System.out.println("feiju1");break;
}
case('b'):{
System.out.println("2");break;
}
default:
System.out.println("=====");
}
}
}
细节讨论:
1.表达式的数据类型,应该和case后的常量类型一致,或者可以自动转换的(比如char和int)
2.switc(表达式)中表达式的返回值必须是byte,short,int,char,enum[],String中的类型。注意enum[]是枚举
3.case子句中必须是常量,或者常量表达式,不能有变量。
4.default是可选的,也可以没有
5.遇到break会退出程序,不遇到会继续执行下面的。
练习:
穿透怎么用啊?好家伙第一次见。
for循环
for(循环变量初始化;循环条件;循环变量迭代){
循环操作;
}
细节
1.循环条件是返回一个布尔值的表达式
2.for(;条件;)中的初始化和变量迭代是可以写到外面的,但是里面的分号不能省。
3.循环初始值可以有多条初始化语句,但是要求类型一样,并且中间用逗号隔开,迭代也可以有多条
4.输出啥?
i=0,j=0
i=1,j=2
i=2,j=4
练习:
1.
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
int j=0,sum=0;
for(int i=1;i<=100;i++){
if(i%9==0){
System.out.println(i);
j++;
sum+=i;
}
}
System.out.println("you zhe me duo ge:"+j+"\nsum is:"+sum);
}
}
2.
public class day7{
public static void main(String[] args) {
for(int i=0,sum=5;i<6;i++,sum--){
System.out.println(i+"+"+sum+"="+(i+sum));
}
}
}
老师这个更妙啊:
while循环
这个之前用的也不太好..基本语法如下.细节:while先判断再执行。
循环变量初始化;
while(循环条件){
循环体(语句);
循环变量迭代;
}
练习:
1.
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
int i = 1;
while(i<=100){
if(i%3==0)
System.out.println(i);
i++;
}
}
}
2.
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
int i = 40;
while(i<=200){
System.out.println(i);
i+=2;
}
}
}
吃个饭回来在搞》。。
do...while循环控制
语法:
循环变量初始化;
do{
循环体;
循环变量迭代;
}while(循环条件);
说明:先执行,再判断,也就是说一定会至少执行一次。
练习:
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
int i = 1;
System.out.println("1st question");
do{
System.out.println(i++);
}while(i<=100);
System.out.println("2nd question");
int k = 1,sum = 0;
do{
sum+=k++;
System.out.println(sum);
}while(k<=100);
System.out.println("3th question");
int j = 1,count = 0;
do{
if(j%5==0&&j%3!=0)
count++;
j++;
}while(j<=200);
System.out.println(count);
System.out.println("4th question");
Scanner myscn = new Scanner(System.in);
char m;
do{
System.out.println("huan qian? y/n");
m = myscn.next().charAt(0);
}while(m!='y');
System.out.println("good boy");
}
}
老师:好一个先兵后礼,不管还不还钱,先打了再说是吧?
多重循环练习(重点)
1.
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
int s1=0,s2=0;
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
System.out.println("input score of "+(i+1)+" class's the "+(j+1)+" student");
s1+=mysc.nextInt();
}
System.out.println("the "+(i+1)+"class's average is "+((double)s1/5));
s2+=s1;
s1=0;
}
System.out.println("all classes's average is "+((double)s2/15));
}
}
2.
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
int s1=0,s2=0;
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
System.out.println("input score of "+(i+1)+" class's the "+(j+1)+" student");
if(mysc.nextInt()>=60)
s1++;
}
System.out.println("the "+(i+1)+"class's ok student is "+s1);
s2+=s1;
s1=0;
}
System.out.println("all classes's ok student is "+s2);
}
}
3.原来System.out.print();就是不换行啊~~
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
int s1=1,s2=1;
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
}
System.out.println("over");
}
}
练习:空心金字塔
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
System.out.println("input the layer?");
//x=layer
int x=mysc.nextInt();
for(int i=1;i<=x;i++){
for(int j=0;j<x-i;j++)
System.out.print(" ");
for(int j=1;j<=2*i-1;j++){
if(j==1||j==2*i-1||i==x)
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
}
}
真累人...讲解有点意思0136_韩顺平Java_空心金字塔_哔哩哔哩_bilibili
尝试打印空心菱形:(成功了,虽然折腾了半天因为少写个等号,然后丢给ai立马跑出来,救命,我真的学得会吗?)
跳转控制语句break
当break语句出现在多层嵌套的语句中时,可以使用标签指明要终止的是哪一层语句块。
例题:
抽卡无保底现状:
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
int x = 0,sun = 0;
do{
x=(int)(Math.random()*100)+1;
if(x==97)
{System.out.println("x is " + x + "! get it need " + sun + " times~");
System.out.println("=============");
break;}
else
System.out.println("x is "+x+" already "+ sun +" times");
sun++;
}while(true);
}
}
练习:
第一题:
结果是6
第二题:
字符串的比较推荐使用:
可以避免空指针。
continue
用于结束本次循环,继续执行下一次循环。和break一样,可以制定label,例:
01010101
如果continue label2的话是013456789循环4次
return
表示跳出所在的方法,在讲解方法的时候,会详细的介绍,如果return写在main里,会退出程序。
本例中,return在主方法(main)中,所以退出程序了,只会输出
hello
hello
韩顺平
本章作业
第一题
贴心的询问了你有多少钱,好收保护费。
import java.util.Scanner;
public class day7{
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
System.out.println("input your money");
double money = mysc.nextDouble();
int count = 0;
while(true){
if(money>50000){
money*=0.95;
count++;
}
else if(money>=1000&&money<=50000){
money-=1000;
count++;
}
else{
break;
}
}
System.out.println(count);
}
}
第二题
import java.util.Scanner;
public class day7 {
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
System.out.println("input your integer~");
int x = mysc.nextInt();
if (x > 0) {
System.out.println("zhengshu");
} else if (x < 0) {
System.out.println("fushu");
} else {
System.out.println("0");
}
mysc.close();
}
}
第三题
- 如果年份能被4整除且不能被100整除,那么它是闰年。
- 如果年份能被400整除,那么它也是闰年。
import java.util.Scanner;
public class day7 {
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
System.out.println("input your year~");
int x = mysc.nextInt();
if ((x % 4==0&&x % 100 != 0)||(x%400==0)) {
System.out.println("run nian");
} else {
System.out.println("no run nian");
}
mysc.close();
}
}
第四题
import java.util.Scanner;
public class day7 {
public static void main(String[] args) {
Scanner mysc = new Scanner(System.in);
System.out.println("input your integer(abc)~");
int x = mysc.nextInt();
int a=0,b=0,c=0;
a=x/100;
b=(x-100*a)/10;
c=(x-100*a-10*b);
if(x==a*a*a+b*b*b+c*c*c){
System.out.println("yes!");
}
else{
System.out.println("no");
}
mysc.close();
}
}
还有这种写法:
学到惹
第五题
啥都不输出啊
第六题
public class day7 {
public static void main(String[] args) {
for(int i=1;i<=100;i++){
int count = 0;
while(count<5){
if(i%5!=0)
{System.out.print(i + "\t");
count++;}
i++;
}
System.out.println();
}
}
}
老师:
不嵌套也可以哈~
第七题
输出的时候可以强制转换一下就不会出数字了!
import java.util.Scanner;
public class day7 {
public static void main(String[] args) {
// Scanner mysc = new Scanner(System.in);
// System.out.println("input your integer(abc)~");
// int x = mysc.nextInt();
char c1 = 'a';
char c2 = 'Z';
for(int i=0;i<26;i++)
System.out.print((char)(c1+i));
System.out.println();
for(int i=0;i<26;i++)
System.out.print((char)(c2-i));
// mysc.close();
}
}
第八题
注意一下sum和flag都是整数,转成double才好算~
import java.util.Scanner;
public class day7 {
public static void main(String[] args) {
// Scanner mysc = new Scanner(System.in);
// System.out.println("input your integer(abc)~");
// int x = mysc.nextInt();
double sum = 0;
int flag=1;
for(int i=1;i<=100;i++){
sum+=(double)flag/i;
flag*=-1;
}
System.out.println(sum);
// mysc.close();
}
}
第九题
import java.util.Scanner;
public class day7 {
public static void main(String[] args) {
// Scanner mysc = new Scanner(System.in);
// System.out.println("input your integer(abc)~");
// int x = mysc.nextInt();
int s1 = 0;
for(int i =1;i<=100;i++){
for(int j=1;j<=i;j++){
s1+=j;
}
}
System.out.println(s1);
// mysc.close();
}
}
这个思路妙啊!