javaSE_day03
目录
1. 控制台输入
Scanner sc = new Scanner(System.in);
String str = sc.next(); //接收字符串型
int num = sc.nextInt(); //接收字整型
float f = sc.nextFloat(); //接收浮点型
double d = sc.nextDouble(); //接收double型
2. 语句种类
2.1 赋值语句
eg: x = 9;
2.2 方法调用语句
方法由对象、类名调用
eg: System.out.print(“hello”); // print是对象
补充: out 是引用变量 ,是个对象
err / out的区别:【System.err.print();】输出的结果字体是红色的。
2.3 import语句(导类)
作用:导入类
格式:import java.util.*; import java.util.Scanner;
位置:class上边,package下边
数量:多个
2.4 package语句(定义包,管理类)
作用:管理类
格式:package cn.tedu.demo;
位置:第一条有效语句
数量:至多一条
2.5 控制语句
- 判断语句:if,switch
- 循环语句:for,while,do-while
- 跳转语句:break ,continue
2.5.1 判断语句
-
if 语句:
1. if(boolean){ }
2. if(boolena) { } else { }
3. if(boolean) { } if(boolean){ } …
4. if(boolean) { } else if(boolean) { } … else {} -
switch语句:
switch( ){
//小括号里可以是byte,short,int,char,String,Emun枚举类型
case n: 语句;
break;(break可有可无,如果有,跳出switch语句,如果没有,继续向下执行,和n无关
…
default: 语句;练习:
public class Demo4 {
//接收键盘输入,如果为偶数打印输出
public static void test1() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入值:");
double num = sc.nextDouble();
if(num%2==0 ) {
System.out.println("输出"+num);
}
}
/**输入一个数字表示重量,如果重量<=20,则每千克收费0.35元;
如果超过20千克不超过100千克的范围,则超过的部分按照每千克0.5元收费;
如果超过100千克,则超过的范围按照每千克0.8元收费。*/
计算输入的重量对应的总费用
public static void test2() {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个数字表示重量:");
double num2 = sc.nextDouble();
double price;
if(num2<=20) {
price = 0.35*num2;
System.out.println("总费用为:"+price);
}else if (num2>20 && num2<=100) {
price = 0.35*20 + (num2-20)*0.5;
System.out.println("总费用为:"+price);
}else if (num2>100) {
price = 0.35*20 + 80*0.5 + (num2-100)*0.8;
System.out.println("总费用为:"+price);
}
}
/**接收 控制台输入的年和月份,输出该年该月有多少天
year%4==0&&year%100!=0||year%400==0闰年判断条件*/
public static void test3() {
Scanner sc = new Scanner(System.in);
System.out.println("输入年:");
int year = sc.nextInt();
System.out.println("输入月份:");
int month = sc.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
case 2:
if(year%4==0 && year%100!=0 || year%400==0) {
System.out.println("29天");
}else {
System.out.println("28天");
}
break;
}
//控制台输入,两个整数,和运算符,输出结果
public static void test4() {
Scanner sc = new Scanner(System.in);
System.out.println("输入数字1:");
int num1 = sc.nextInt();
System.out.println("输入运算符:");
String str = sc.next();
System.out.println("输入数字2:");
int num2 = sc.nextInt();
switch(str){
case "+":
System.out.println(num1+num2);
break;
case "-":
System.out.println(num1-num2);
break;
case "*":
System.out.println(num1*num2);
break;
case "/":
System.out.println(num1/num2);
break;
}
}
public static void main(String[] args) {
//test1();
//test2();
test3();
//test4();
}
}
2.5.2 循环语句
for语句:
-
for(表达式1;表达式2;表达式3){
语句体;
}循环原理:
-
表达式1:完成初始化
-
表达式2:判断条件,如果为true,执行语句体;
-
表达式3:(自增自减(步长表达式)),执行表达式2,直到判断条件为false时,跳出循环体
eg:int sum = 0; for( int i = 1; i<=10; i++){ //有固定次数时选择for循环 sum += i; } system.out.println(sum);
-
while语句:
-
while(boolean){
循环体;
}
eg:int sum = 0; int i = 1; while(i<=10){ //没有固定次数时选择for循环 sum += i; i++; } system.out.println(sum);
*do语句:
- do{ //至少执行一次
循环体;
}while(boolean);
练习:
public class Demo5 {
//1-100内奇数和
public static void test1() {
int sum = 0;
for (int i = 1; i <=100; i++) {
if(i%2==1) {
sum += i;
}
}
System.out.println(sum);
}
//100以内被3整除而不能被7整除的数字
public static void test2() {
for(int i=1;i<=100;i++) {
if(i%3==0 && i%7!=0)
System.out.println(i);
}
}
//输入一个数字,输出这个数字是一个几位数
public static void test3() {
Scanner sc = new Scanner(System.in);
System.out.println("input:");
int num = sc.nextInt();
int count = 1;
while(num>=10) {
num = num / 10;
count++;
}
System.out.println("输入的数是"+count+"位数");
}
//输入一个数字,输出它的所有因数
public static void test4() {
Scanner sc = new Scanner(System.in);
System.out.println("input:");
int num = sc.nextInt();
for (int i =1; i <=num; i++) {
if (num%i==0) {
System.out.println(i);
}
}
}
public static void main(String[] args) {
//test1();
//test2();
//test3();
test4();
}
}
2.5.3 嵌套循环语句 (*百钱百鸡)
-
eg:
for(int i= 1;i<=10;i++){
for(int j= 1;j<=5;j++){
System.out.print(“hello”);
}
}
循环原理: 外层执行一次,内层执行全部 -
经典案例: 百钱百鸡
//百钱百鸡
//3文钱1只公鸡,2文钱1只母鸡,1文钱3只小鸡;100文钱如何买恰好100只鸡?
public static void test3() {
//公鸡最多数
for (int i = 1; i<=33; i++) {
//母鸡最多数
for (int j = 1; j <=50; j++) {
//小鸡数目
int k = 100-i-j;
if (k%3==0 && i*3+j*2+k/3 == 100) {
System.out.println(i+"只公鸡"+j+"只母鸡"+k+"只小鸡");
}
}
}
}
练习: 九九乘法表
public class Demo6 {
//打印6行*****
public static void test1() {
for(int i=1;i<=5;i++) {
for (int j = 1; j <= 6; j++) {
System.out.print("*");
}
System.out.println();
}
}
//打印阶梯*
public static void test2() {
for(int i=1;i<=5;i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
//九九乘法表
public static void test3() {
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();
}
}
public static void main(String[] args) {
//test1();
//test2();
test3();
}
}
2.5.3 跳转语句
break:
- 用于switch和循环体中
- 表示跳出循环体(switch语句体)
- break语句后的代码永远不能执行
continue:
- 用于循环体中
- 表示结束本次循环,开始新一次循环
- continue语句后的代码永远不能执行
2.5.4 标号语句
给内层循环标号无意义。
eg:
public class Demo7 {
//标号语句
public static void main(String[] args) {
/*for (int i = 1; i <=5; i++) {
for (int j = 1; j <=4; j++) {
System.out.print(j+"\t");
}
System.out.println();
}*/
/*for (int i = 1; i <=5; i++) {
for (int j = 1; j <=4; j++) {
if (j==2) {
break ; //跳出里层循环
}
System.out.print(j+"\t");
}
System.out.println();
}*/
out:for (int i = 1; i <=5; i++) {
for (int j = 1; j <=4; j++) {
if (j==2) {
break out; //跳出外层循环
}
System.out.print(j+"\t");
}
System.out.println();
}
}
}
练习:
public class Demo8 {
//输出2-1000所有的质数
public static void test1() {
for (int i = 2; i<=1000; i++) {
int flag = 0;
for (int j = 2; j<i; j++) {
if (i%j==0) {
flag=1;
break ;
}
}
if(flag == 0) {
System.out.println(i+"\t");
}
}
}
//求4的阶乘的和 : 4!+3!+2!+1!
public static void test2() {
int sum = 0;
for (int i = 1; i<=4; i++) {
int r = 1;
for (int j = 1; j <=i; j++) {
r*= j;
}
sum += r;
}
System.out.println(sum);
}
public static void main(String[] args) {
//test1();
//test2();
test3();
}
}