Scanner对象
实现人机交互,java.util.Scanner是java5的新特性,我们可以通过Scanner类来获取用户输入。
基本语法:
Scanner scanner = new Scanner(System.in);
new 一个类后自动返回类对象定义:Alt + Enter
package com.liu.Scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入
if (scanner.hasNext()){
//使用next()方式接收
String str = scanner.next();
System.out.println("输出的内容为:" + str);
}
//凡是属于IO流的类可以关闭。用完scanner可以关闭,节省资源
scanner.close();
}
}
package com.liu.Scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//new 一个类后自动返回类对象定义:Alt + Enter
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine接收:");
if (scanner.hasNextLine()){
String str = scanner.next();
System.out.println("输出:" + str);
}
scanner.close();
}
}
package com.liu.Scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,没输入一个数字回车确认,通过输入非数字结束
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
int num = 0;
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
num++;
sum+=x;
System.out.println("你输入了第" + num + "个数据,然后当前结果sum为:" + sum);
}
System.out.println(num + "个数的和为:" + sum);
System.out.println(num + "个数的平均值为:" + sum/num);
scanner.close();
}
}
流程控制
1、顺序结构
顺序
package com.liu.struct;
public class shunXuDemo {
public static void main(String[] args) {
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
}
}
2、选择结构
if
package com.liu.struct;
import java.util.Scanner;
public class ifDemo01 {
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();
}
}
if-else
package com.liu.struct;
import java.util.Scanner;
public class ifDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//if单选择
//考试分数大于60就及格,否则不及格
int score = scanner.nextInt();
if (score > 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
if-else-if
package com.liu.struct;
import java.util.Scanner;
public class ifDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//if-else-if多选择
/*
至少有一个else,且在所有else-if之后
可以有若干个else-if
一旦有一个else-if为true,其他的else-if和else跳过执行
*/
//考试分数
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();
}
}
package com.liu.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透
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("未知等级");
}
}
}
String类型:
package com.liu.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
// jdk7后,表达式结果可以是字符串了!!!
// 字符的本身还是数字
// 反编译 java---class(字节码文件) ---反编译(IDEA)
String name = "liu ting";
switch (name){
case"liu ting":
System.out.println("liu ting");
break;
case"liu":
System.out.println("liu");
break;
case"li":
System.out.println("li");
break;
default:
System.out.println("liu ting");
break;
}
}
}
反编译:
#####3、循环结构
1、while:
package com.liu.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1--100
int i = 0;
while(i < 100){
i++;
System.out.println(i);
}
}
}
package com.liu.struct;
public class WhileDemo02 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while(i<=100){
sum+=i;
i++;
}
System.out.println(sum); //5050
}
}
2、do—while
package com.liu.struct;
public class DoWhileDemo {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do{
sum+=i;
i++;
}while(i<=100);
System.out.println(sum); //5050
System.out.println("=======================");
//两个对比
int a = 0;
while(a < 0){
System.out.println(a);// 无
a++;
}
do{
System.out.println(a); // 0
a++;
}while(a < 0);
}
}
3、for循环
100.for—自动生成
package com.liu.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1;//初始条件
while(a<=100){ //条件判断
System.out.println(a); //循环体
a+=2; //迭代
}
System.out.println("while循环结束!============");
for(int i=1;i<100;i++){
System.out.println(i);
}
System.out.println("for循环结束!============");
}
}
练习1:计算1–100之间奇数和偶数的和
package com.liu.struct;
public class ForDemo02 {
//计算0---100之间奇数和偶数的和
public static void main(String[] args) {
int oddsum = 0; //奇数
int evensum = 0; //偶数
for (int i = 1; i <= 100; i++) {
if(i % 2 != 0){
oddsum+=i;
}else{
evensum+=i;
}
}
System.out.println("奇数的和:" + oddsum); //2500
System.out.println("偶数的和:" + evensum); //2550
}
}
练习2:用while或者for循环输出1-1000之间能被5整除的数,并且每行输出三个
package com.liu.struct;
public class ForDemo03 {
public static void main(String[] args) {
// 用while或者for循环输出1-1000之间能被5整除的数,并且每行输出三个
int num = 0;
for (int i = 1; i <= 1000; i++) {
if(i % 5 == 0){
num++;
System.out.print(i + " ");
// if(num % 3 == 0){
// System.out.print("\n");
// }
if(i % (5*3) == 0){
System.out.println();
}
}
}
//println换行 print不换行
}
}
练习3:打印九九乘法表
package com.liu.struct;
public class ForDemo04 {
//打印九九乘法表
/*
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
*/
public static void main(String[] args) {
// 1、打印第一列
// 2、我们把固定的 1 再用一个循环包起来
// 3、去掉重复项 i <= j
// 4、调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j + "*" + i + "=" + (j * i) + "\t");
}
System.out.println();
}
}
}
4、增强for循环
package com.liu.struct;
public class ForDemo05 {
// 增强for循环
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
//遍历数组元素
//简化
for (int x: numbers){
System.out.println(x);
}
System.out.println("==================");
//老土
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
}
}
5、break—continue
break
package com.liu.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while(i<100){
i++;
System.out.println(i);
if (i==10){
break;
}
}
System.out.println("123");
}
}
continue
package com.liu.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i=0;
while(i<30){
i++;
if (i%10==0){
System.out.println();
continue; //回到while循环开始的地方
}
System.out.print(i + "\t");
}
}
/*
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
*/
}
goto:
package com.liu.struct;
public class LabelDemo {
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.print(i + " "); //101 103 107 109 113 127 131 137 139 149
}
}
}
练习
打印三角形
package com.liu.struct;
public class TestDemo {
//打印三角形
public static void main(String[] args) {
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("*");
}
System.out.println();
}
}
/*
*
***
*****
*******
*********
*/
}