Scanner对象
next()和nextLine()
next()
package com.jiao.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()){
String str = scanner.next();
System.out.println("输入的内容为:"+str);
}
scanner.close();
}
}
输出
使用next发法接收:
hello world
输入的内容为:hello
nextLine()
package com.jiao.scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用next发法接收:");
if(scanner.hasNext()){
String str = scanner.nextLine();
System.out.println("输入的内容为:"+str);
}
scanner.close();
}
}
输出
使用next发法接收:
hello world
输入的内容为:hello world
package com.jiao.scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据:");
String s = scanner.nextLine();
System.out.println("输出内容为:"+s);
scanner.close();
}
}
输出
请输入数据:
欢迎大家 学习 java
输出内容为:欢迎大家 学习 java
练习:我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
package com.jiao.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;
System.out.println("请开始输入:");
while (scanner.hasNextDouble()){
double v = scanner.nextDouble();
m = m + 1;
sum = sum + v;
System.out.println("你输入了第"+m+"个数据,然后当前的结果是sum="+sum);
}
System.out.println(m + "个数的和为" + sum);
System.out.println(m + "个数的平均值为" + (sum / m));
scanner.close();
}
}
输出
请开始输入:
2
你输入了第1个数据,然后当前的结果是sum=2.0
88
你输入了第2个数据,然后当前的结果是sum=90.0
911
你输入了第3个数据,然后当前的结果是sum=1001.0
l
3个数的和为1001.0
3个数的平均值为333.6666666666667
顺序结构
package com.jiao.struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
}
}
输出
hello1
hello2
hello3
hello4
选择结构
package com.jiao.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();
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
输出
请输入一个内容:
Hello
Hello
End
package com.jiao.struct;
import java.util.Scanner;
public class IfDemo02 {
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();
}
}
输出
请输入成绩:
50
不及格
package com.jiao.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 >= 60){
System.out.println("C");
}else if(score < 60 && score >= 0){
System.out.println("不及格");
}else{
System.out.println("输入成绩不合法");
}
scanner.close();
}
}
输出
请输入成绩:
80
B
package com.jiao.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'B';
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;
default:
System.out.println("未知等级");
}
}
}
输出
良好
jdk7新特性
package com.jiao.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "李明";
switch (name){
case "张三":
System.out.println("张三");
break;
case "李明":
System.out.println("李明");
break;
default:
System.out.println("输入不合法");
}
}
}
输出
李明
循环结构
package com.jiao.struct;
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
while (i < 100){
i++;
System.out.println(i);
}
}
}
输出
1
2
、、、
100
package com.jiao.struct;
public class WhileDemo02 {
public static void main(String[] args) {
while (true){
}
}
}
计算1+2+3+…+100 = ?
package com.jiao.struct;
public class WhileDemo03 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i <= 100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
输出
5050
package com.jiao.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do{
sum = sum + i;
i++;
}while (i <= 100);
System.out.println(sum);
}
}
输出
5050
package com.jiao.struct;
public class DoWhileDemo02 {
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
package com.jiao.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:计算0到100间的奇数和偶数的和
package com.jiao.struct;
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);
}
}
输出
奇数的和2500
偶数的和2550
练习2:用while或for循环输出1~1000间能被5整除的数,并且每行输出3个
package com.jiao.struct;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if(i % 5 == 0){
System.out.print(i+"\t");
}
if(i % (5*3) == 0){
System.out.println("\n");
}
}
}
}
输出
0
5 10 15
20 25 30
35 40 45
50 55 60
、、、
练习3:输出9X9乘法表
package com.jiao.struct;
public class ForDemo04 {
public static void main(String[] args) {
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();
}
}
}
package com.jiao.struct;
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);
}
}
}
输出
10
20
30
40
50
=============
10
20
30
40
50
|