以下内容学习于狂神说Java,b站视频
由于技术问题,图片可能错位,有问题请联系博主修改
之前学习的基本语法中并没有实现程序和人的交互,但是Java提供了一个工具类,可以获取用户的输入。java.util.Scanner是jdk5的新特性,可以通过Scanner类来获取用户的输入。
基本语法:
Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前一般需要使用 hasNext()与hasNextLine()判断是否还有输入的数据。
也可以不进行判断直接使用next()与nextLine()方法获取输入的字符串。
注意以下内容:
//可以使用int long double float
1、一定要读取到有效字符之后才可以结束输入。
2、对于输入有效字符之前遇到的空白,next()方法会自动去掉。
3、直到检测到第一个有效字符后,才将其后面输入的空白作为分隔符或者结束符。
4、next()不能得到带有空格的字符串。
package com.LinYIN.Scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next()方式接收:");
//hasNext()判断用户有没有输入字符串
if (scanner.hasNext()){
//使用next()方式接收
String str = scanner.next();//程序会等待用户输入完毕
System.out.println("输出的内容为:"+str);
}
//凡是属于IO流的类如果不关闭则会一直占用资源
//切记用完之后必须要用scanner.close();语句关闭
scanner.close();
}
}
1、以Enter为结束符,nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空格符。
package com.LinYIN.Scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine()方式接收:");
//hasNextLine()判断用户有没有输入字符串
if (scanner.hasNextLine()){
//使用nextLine()方式接收
String str = scanner.nextLine();//程序会等待用户输入完毕
System.out.println("输出的内容为:"+str);
}
//凡是属于IO流的类如果不关闭则会一直占用资源
//一定要时刻记住用完之后必须要用scanner.close();语句关闭
scanner.close();
}
}
接收数字:
package com.LinYIN.Scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//从键盘接收数字
int i = 0;
float f = 0.0f;
System.out.println("请输入整数");
if(scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:" + i);
}else{
System.out.println("输入的不是整数!");
}
System.out.println("请输入小数");
if(scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据:" + f);
}else{
System.out.println("输入的不是小数!");
}
scanner.close();
}
}
package com.LinYIN.Scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//输入多个数字,并求其总和与平均数
//每输入一个数字用回车确认,通过输入非数字来结束输出执行结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入了多少个数字
int m = 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m + 1;//m++
sum = sum + x;
System.out.println("你输入了第" + m + "个数,当前结果为:" + sum);
}
System.out.println(m + "个数的和为:" + sum);
System.out.println(m + "个数的平均值为:" + (sum/m));
scanner.close();
}
}
package com.LinYIN.Scanner;
import sun.awt.windows.WPrinterJob;
public class Demo06 {
public static void main(String[] args) {
int i = 0;
System.out.print("print输出不换行:");
while (i<5){
i++;
System.out.print(i);
}
System.out.println();
System.out.println("println输出换行:");
int j = 0;
while(j<5){
j++;
System.out.println(j);
}
}
}
顺序结构是最简单的算法结构
语句与语句之间,框与框之间是按从上到下的顺序进行的,由若干个依次执行的处理步骤组成,任何一种算法都离不开顺序结构
package com.LinYIN.struct;
public class ShunXuDemo {
public static void main(String[] args){
System.out.println("这是第一步");
System.out.println("这是第二步");
System.out.println("这是第三步");
System.out.println("这是第四步");
System.out.println("这是第五步");
System.out.println("这是第六步");
}
}
语法:
if(布尔表达式){
//如果布尔表达式为true则执行这个花括号内的语句
语句1;
语句2;
语句3;
}
package com.LinYIN.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("布尔表达式成立");
}
scanner.close();
}
}
语法:
if(布尔表达式){
语句1;
}else{
语句2;
}
//如果布尔表达式为true则执行语句1
//如果布尔表达式为false则执行语句2
package com.LinYIN.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//输入成绩,如果大于60则输出及格,否则输出不及格
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if(score>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
if(布尔表达式1){
语句1;//如果布尔表达式1为true,则执行语句1,剩下的语句不执行
}else if(布尔表达式2){
语句2;//如果布尔表达式1为false,布尔表达式2为true,则执行语句2,剩下的语句不执行
}else if(布尔表达式3){
语句3;//如果布尔表达式1为false,布尔表达式2为false,布尔表达式3为true,则执行语句3,剩下的语句不执行
}else{
语句4;//如果上述布尔表达式都为false,则执行语句4,剩下的语句不执行
}
package com.LinYIN.struct;
import java.util.Scanner;
public class IfDemo03 {
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();
}
}
语法:
if(布尔表达式1){
语句1;
if(布尔表达式2){
语句2;
}
}
switch(expression){
case value1:
语句1;
break;//break可以不加,但建议加上
case value2:
语句2;
break;
default: //default可选
语句3;
}
package com.LinYIN.struct;
import java.util.Scanner;
public class SwitchDemo01 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("输入字母:");
String grade = scanner.nextLine();
//jdk7以后支持字符串比较
switch(grade){
case "ABC":
System.out.println("你输入了ABC");
break;
case "BCD":
System.out.println("你输入了BCD");
case "CDE":
System.out.println("你输入了CDE");
case "DEF":
System.out.println("你输入了DEF");
default:
System.out.println("未匹配成功");
}
scanner.close();
}
}
package com.LinYIN.struct;
import java.util.Scanner;
public class SwitchDemo01 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("输入字母:");
String grade = scanner.nextLine();
//jdk7以后支持字符串比较
switch(grade){
case "ABC":
System.out.println("你输入了ABC");
//break;
case "BCD":
System.out.println("你输入了BCD");
case "CDE":
System.out.println("你输入了CDE");
case "DEF":
System.out.println("你输入了DEF");
default:
System.out.println("未匹配成功");
}
scanner.close();
}
}
while(布尔表达式){
//循环内容
}
package com.LinYIN.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~10
int i = 0;
while(i<10){
i++;
System.out.println(i);
}
}
}
package com.LinYIN.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true) {
System.out.println("死循环");
}
}
}
package com.LinYIN.struct;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+...+100
int i = 0;
int sum = 0;
while(i<=100){
sum += i;
i++;
}
System.out.println(sum);
}
}
do{
//代码语句
}while(布尔表达式);
package com.LinYIN.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
while(i<0){
System.out.println("while输出i值:"+i);
i++;
}
do {
System.out.println("do...while输出i值:"+i);
i++;
}while(i<0);
}
}
for(初始化;布尔表达式;更新){
//代码语句
}
package com.LinYIN.struct;
public class ForWhileDemo01 {
public static void main(String[] args) {
//初始化//条件判断//迭代
for(int i = 0; i <= 10; i++){
System.out.println(i);
}
System.out.println("for循环结束!");
}
}
package com.LinYIN.struct;
public class ForDemo02 {
public static void main(String[] args) {
//计算0~100之间奇数和偶数的和
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);
}
}
package com.LinYIN.struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1~1000之间能被5整除的数,并每行输出3个
for (int i = 0; i <= 100; i++) {
if (i%5==0){
//判断能被5整除的数
System.out.print(i+"\t");
}
if (i%(5*3)==0){
//每3个换行
System.out.println();
}
}
}
}
package com.LinYIN.struct;
public class ForDemo04 {
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 + "=" + (i*j) + "\t");
}
System.out.println();
}
}
}
package com.LinYIN.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
int j = 0;
while(i<100){
i++;
System.out.print(i+"\t");
if(i%10==0){
j++;
System.out.println();
System.out.println("第"+j+"次i="+i+"使得i%10==0条件判断成立,终止循环");
break;//break强制退出循环,不执行剩余的循环语句
}
}
System.out.println("循环结束");
}
}
package com.LinYIN.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
int j = 0;
while(i<100){
i++;
System.out.print(i+"\t");
if(i%10==0){
j++;
System.out.println();
System.out.println("第"+j+"次i="+i+"使得i%10==0条件判断成立,终止本次循环");
continue;//continue只是终止本次条件下循环过程,跳过未执行的语句,接着进行下一次是否执行循环的判断
}
}
System.out.println("循环结束");
}
}