Scanner
package Scanner;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
String str = scanner.nextLine();
System.out.println("输出的内容为: "+str);
//属于IO流的类如果不关闭会一直占用资源
scanner.close();
}
}
int a = scanner.nextInt();
double b = scanner.nextDouble();
next():
- 一定要读取到有效字符后才开始结束输入;
- 输入有效字符后才将其后面输入的空白作为分隔符或者结束符;
- next()不能得到带有空格的字符串;
nextLine():
- 以ENTER为结束符,nextLine()方法返回的是输入回车之前的所有字符;
- 可以获得空白;
选择结构
- if单选择
- if-else双选择
- if-else if-else多选择
- switch
- JDK支持了Srtring类型
- case穿透现象
- break
- default
if单选泽
package Struct;
import java.util.Scanner;
public class IfDemo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("end");
scanner.close();
}
}
if-else双选择
package Struct;
import java.util.Scanner;
public class IfDemo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的成绩");
int score = scanner.nextInt();
//
if (score <= 60){
System.out.println("不及格");
}else{
System.out.println("及格");
}
scanner.close();
}
}
if-else if-else多选择
package Struct;
import java.util.Scanner;
public class IfDemo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的成绩");
//int score = scanner.nextInt();
float score = scanner.nextFloat();
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();
}
}
switch
package Struct;
import java.util.Scanner;
public class IfDemo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的成绩");
//int score = scanner.nextInt();
float score = scanner.nextFloat();
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();
}
}
package Struct;
public class Switch2 {
public static void main(String[] args) {
String name = "卡莎";
//JDK7的新特性 表达式结果可以是字符串
//字符的本质还是数字
//反编译 java---class(字节码文件)---反编译(IDEA)
switch (name){
case "卡莎":
System.out.println("虚空之女");
break;
case "崔丝塔娜":
System.out.println("麦林炮手");
break;
case "艾希":
System.out.println("寒冰射手");
break;
default:
System.out.println("含羞蓓蕾");
}
}
}
循环结构
- while & do while
- for
- for-each增加for循环
while & do while
- while先判断再执行
- do while先执行再判断
package Struct;
public class DoWhileDemo1 {
public static void main(String[] args) {
int i = 0;
while(i<0){
i++;
System.out.println(i);//没有执行
}
do {
i++;
System.out.println(i);//执行了两次
}while (i<2);
}
}
for
100以内奇数和偶数的和
package Struct;
public class ForDemo2 {
public static void main(String[] args) {
int evenSum = 0;
int oddSum = 0;
for (int i = 0; i <= 100; i++) {
if (i%2==0){
//偶数
evenSum = evenSum + i;
}else{
//奇数
oddSum = oddSum + i;
}
}
System.out.println("偶数和为:"+evenSum);
System.out.println("奇数和为:"+oddSum);
}
}
package Struct;
public class ForDemo3 {
public static void main(String[] args) {
//输出1000以内可以被5整除的数,每行三个
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%15==0) {//3×5
System.out.println("");
}
//System.out.print不换行输出
//System.out.println换行输出
}
}
九九乘法表
package Struct;
public class ForDemo5 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
}
增加for循环
package Struct;
public class ForDemo6 {
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
package Struct;
public class BreakContinueDemo1 {
public static void main(String[] args) {
//break用于强行退出循环
//continue用于终止某次循环过程
int i = 0;
while(i<=100){
i++;
if (i%10==0){
System.out.println(i);
break;
}
}
System.out.println(i);
}
}
练习
Test1 打印三角形
思路:
package Struct;
public class TestDemo1 {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
for (int j = 5; j >= i; j--) {//可以不写
System.out.print(" ");
}
System.out.println();
}
}
}
输出:
*
***
*****
*******
*********
Test2 质数
- 标签的方法
package Struct;
public class LabeDemo1 {
public static void main(String[] args) {
//打印101-150之间所有的质数
int count = 0;
outer:for (int i = 101; i <= 150; i++) {
for (int j = 2; j < i/2; j++) {
if (i%j==0){//不是质数
continue outer;
}
}
System.out.println(i);
}
}
}
- 布尔值flag
package Struct;
public class LabeDemo2 {
public static void main(String[] args) {
//101到150以内的质数;
for (int i = 101; i <= 150; i++) {
boolean flag = true;
for (int j = 2; j < i/2; j++) {
if (i%j==0){
//不是质数
flag = false;
}
}
if(flag){
System.out.print(i+" ");
}
}
}
}
输出:
101 103 107 109 113 127 131 137 139 149
Process finished with exit code 0