Java学习Day03 Scanner 和各种结构
Scanner
//定义
Scanner scanner = new Scanner(System.in);
scanner close();//关闭io节省资源
//作用是接收用户键盘的数据
public class Dome01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
if(scanner.hasNext()){
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
scanner.close();
//凡是io流一定要关闭 否则会占用资源
}
}
//next方式接收不到空格以后的数据
使用next方式接收:
10 20
输出的内容为:10
public class Dome02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("用nextLine方式接收:");
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输出的内容为:"+str);
}
scanner.close();
}
}
//nextLine 可以接收空格后的数据
用nextLine方式接收:
10 20
输出的内容为:10 20
顺序结构
大部分Java程序都是顺序结构 除非特别指明,都是从上往下
是任何一个算法都离不开的结构
public class SunXuJieGou {
public static void main(String[] args) {
System.out.println("hello world1");
System.out.println("hello world2");
System.out.println("hello world3");
System.out.println("hello world4");
System.out.println("hello world5");
}
}
hello world1
hello world2
hello world3
hello world4
hello world5
选择结构
If单选择
public class IfDome01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容: ");
String s = scanner.nextLine();
//equals :判断字符串是否相等
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
请输入内容:
hello
hello
End
IF双选择
public class IfDome02 {
public static void main(String[] args) {
//考试分数大于60 及格小于就不及格
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
if (score>60){
System.out.println("及格");
}
else {
System.out.println("不及格");
}
}
}
80
及格
IF多选择
public class IfDome03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if(score==100){
System.out.println("恭喜满分");
} else if (score < 100 && score >= 90) {
System.out.println("A");
} else if (score < 90 && score >= 80) {
System.out.println("B");
} else if (score < 80 && score >= 70) {
System.out.println("C");
} else if (score < 70 && score >= 60) {
System.out.println("D");
} else if (score < 60 && score >= 0) {
System.out.println("不及格");
}
else{
System.out.println("成绩不合法");
}
scanner.close();
}
}
请输入成绩
80
B
IF嵌套
Switch多选择
public class SwitchDemo01 {
public static void main(String[] args) {
//Switch 匹配 case 穿透
//switch 会为目标匹配一个值,如果没有break ,case会把所有第一个匹配之后的值都输出;
//break 起到一个打断的作用
char grade = 'C';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("再接再厉");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知");
}
}
}
及格
循环结构
一般情况会写一个情况让循环停止
while循环
至少执行一次
public class WhileDemo03 {
public static void main(String[] args) {
//calculate 1+2+3...+100
int i = 0;
int sum = 0;
while(i<100){
i++;
sum=sum + i;
}
System.out.println(sum);
}
}
100
do…while循环
可以一次不执行
public class DoWhileDemo01 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("================");
do {
System.out.println(a);
a++;
}while(a<0);
}
}
================
0
For循环
public class ForDemo01 {
public static void main(String[] args) {
int a = 1;//初始化条件
while(a<=100){
System.out.println(a);
a+=2;//迭代
}
System.out.println("结束");
// 初始化 条件判断 迭代
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
System.out.println("for结束");
//100.for
//先初始化,可以使空语句
//检测布尔表达式 true 执行循环体 false 终止
//死循环 for (;;){}
}
}
Homework
//0~100奇数偶数和
public class ForDemo02 {
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if(i%2!=0){
oddSum+=i;
}else {
evenSum+=i;
}
}
System.out.println("奇数和:"+oddSum);
System.out.println("偶数和:"+evenSum);
}
}
//输出1~1000被5整除的数,并且每行3个
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){
System.out.println();
//System.out.print("\n");
}//换行
//println输出完会换行
//print 输出完不换行
}
}
}
//输出99乘法表
public class ForDemo04 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int n = 1; n <= i; n++) {
System.out.print(i+"*"+n+"="+i*n +"\t");
//"\t"一个输出给一个空格
}
System.out.println();
换行
}
}
}
附加
增强for循环
public class ForDemo05 {
public static void main(String[] args) {
int [] numbers = {10,20,30,40,50};
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
System.out.println("=================");
for(int x:numbers){
System.out.println(x);
}//简化
}
}
break continue
break 跳出循环但不终止程序
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while(i < 100){
i++;
System.out.println(i);
if (i == 30) {
break;
}
}
}
}
continue 终止一次循环
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10 == 0) {
System.out.println();
continue;
}
System.out.print(i);
}
}
}