1.用户交互Scanner
· 我们可以通过Scanner类来获取用户的输入
· 基本用法:
Scanner s = new Scanner(System.in)
· 通过Scanner类的next()和nextLine()方法获取输入的字符串,在读取前一般使用hasNext()和hasNextLine()判断是否还有输入的数据。
package com.dyo.scanner;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.Scanner;
public class demo1 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
// System.out.println("使用next方法接收:");
System.out.println("使用hasNextLine方法接收:");
//判断用户有没有输入字符
// if(scanner.hasNext()){
if(scanner.hasNextLine()){
// String str = scanner.next();
String str = scanner.nextLine();
System.out.println("输出内容为:"+str);
//next输出hello,nextLine()输出hello world
}
//凡是IO流的类如果不关闭会一直占用资源
scanner.close();
}
}
· next()
- 定要读取到有效字符才可结束输入
- 对输入有效字符之前遇到的空白,next()会自动丢弃
- 只有输入有效字符才将其输入的空白字符作为分隔符
- next()不能得到带有空格的字符串
· nextLine()
- 以Enter为结束符,就是说,nextLine()方法返回的是输入回车符之前的所有字符
- 可以获得空白。
*scanner进阶使用
package com.dyo.scanner;
import java.util.Scanner;
public class demo3 {
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;
sum = sum + x;
}
System.out.println(m + "个数的和为" + sum);
System.out.println(m + "个数的平均值为" + (sum/m));
scanner.close();
}
}
2.顺序结构
Java的基本结构就是顺序结构
3.选择结构
*if单选择结构
package com.dyo.structure;
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双选择结构
package com.dyo.structure;
import java.util.Scanner;
public class ifdemo2 {
public static void main(String[] args) {
//考试大于60是及格,小于60是不及格
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
if(score>60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
- if多选结构
package com.dyo.structure;
import java.util.Scanner;
public class ifdemo2 {
public static void main(String[] args) {
//考试大于60是及格,小于60是不及格
Scanner scanner = new Scanner(System.in);
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>=70){
System.out.println("B");
}else if (score<70 && score>=60){
System.out.println("C");
}else if (score<60 && score>=0){
System.out.println("不及格");
}else{
System.out.println("成绩不合法");
}
scanner.close();
}
}
- if嵌套结构
- switch多选择结构
switch语句中的变量类型可以是:- byte、short、int、char
- 从 JavaSE开始,switch支持字符串类型
- case标签必须为字符串常量或字面量
package com.dyo.structure;
public class SwitchDemo2 {
public static void main(String[] args) {
String name = "小白dyo";
//字符的本质还是数字
//表达式可以是字符串
switch (name){
case "小白dyo":
System.out.println("小白dyo");//输出
break;
case "dyo":
System.out.println("dyo");
break;
default:
System.out.println("干啥呢");
}
}
}
4.循环结构
- while循环
- `package com.dyo.structure;
public class WhileDemo {
public static void main(String[] args) {
// //while循环
// //输出1-100
// int i = 0;
// while(i<100){
// i++;
// System.out.println(i);
// }
//while(true){
//等待客户端连接
//定时检查
//......
//
// }
//计算1-100的和
int i = 0;
int sum = 0;
while(i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
`
- do…while循环
while先判断后执行,do…while先执行后判断
do…while循环体至少会被执行一次!
package com.dyo.structure;
public class DoWhileDemo {
public static void main(String[] args) {
int a = 0;
while(a<0){
System.out.println(a);//不输出
}
System.out.println("------------------");
do{
System.out.println(a); //0
}while(a<0);
}
}
- for循环
package com.dyo.structure;
public class ForDemo {
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 = oddSum + i;
}else{
evenSum = evenSum +i;
}
}
System.out.println(oddSum+"和"+evenSum);
//用while或for循环输出1-1000之间能被5整除的数,并每行输出3个
for (int j = 0; j < 1000; j++) {
if(j%5==0){
System.out.print(j+'\t');
}
if (j%(5*3)==0){
System.out.println();
}
//println输出会换行
//print输出不会换行
}
//打印九九乘法表
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();
}
}
}
- 在Java5中引入一种主要用于数组的增强型for循环
5.break&continue
——break在任何循环语句的主体部分,均可用break控制循环的流程;break用于强行退出循环,不执行循环中剩下的语句。
——continue用于循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行的判断
——关于goto关键
6.练习
```java
package com.dyo.structure;
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();
}
}
}