Scanner

使用示例:

  1. public class DemoSanner02 {
  2. public static void main(String[] args) {
  3. //创建一个扫描器的对象,用于接收键盘数据
  4. Scanner scanner = new Scanner(System.in);
  5. System.out.println("使用next方式接收:");
  6. if(scanner.hasNext()) {
  7. //使用next接收
  8. String str = scanner.next();//只接受空格前一个单词
  9. System.out.println("输出的内容为:" +str);
  10. }
  11. //判断用户有没有输入字符串
  12. if(scanner.hasNextLine()) {
  13. //使用next接收
  14. String str = scanner.nextLine();//接受一行单词
  15. System.out.println("输出的内容为:" +str);
  16. }
  17. //凡是使用IO流的对象,用完必须关闭
  18. scanner.close();
  19. }
  20. }
  1. Scanner scanner = new Scanner(System.in);
  2. //从键盘接收数据:
  3. int i = 0;
  4. float f = 0.0f;
  5. System.out.println("请输入整数:");
  6. if(scanner.hasNextInt()) {
  7. i = scanner.nextInt();
  8. System.out.println("整数数据:" + i);
  9. }
  10. else {
  11. System.out.println("输入的不是整数数据!");
  12. }
  13. System.out.println("请输入数:");
  14. if(scanner.hasNextFloat()) {
  15. f = scanner.nextFloat();
  16. System.out.println("小数数据:" + f);
  17. }
  18. else {
  19. System.out.println("输入的不是小数数据!");
  20. }
  21. scanner.close();
  1. public class ScannerPractice {
  2. public static void main(String[] args) {
  3. //输入多个数字,求和,遇到非数字退出系统
  4. Scanner scanner = new Scanner(System.in);
  5. //和
  6. double sum = 0;
  7. int num = 0;
  8. while(scanner.hasNextDouble()) {
  9. double x = scanner.nextDouble();
  10. num += 1;
  11. sum += x;
  12. }
  13. double avg = sum / num;
  14. System.out.println("和是:"+sum);
  15. System.out.println("平均值:"+avg);
  16. scanner.close();
  17. }
  18. }

顺序、选择、循环结构:

  1. package Sequence;
  2. import java.util.Scanner;
  3. public class demo01 {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. System.out.println("请输入内容:");
  7. if (scanner.hasNextInt()) {
  8. int score = scanner.nextInt();
  9. if (score > 60) {
  10. System.out.println("及格!");
  11. }
  12. else {
  13. System.out.println("不及格!");
  14. }
  15. }
  16. else if (scanner.hasNextDouble()) {
  17. System.out.println("成绩不是整数!");
  18. }
  19. else {
  20. System.out.println("不是数字");
  21. }
  22. scanner.close();
  23. }
  24. }
  1. public class For {
  2. public static void main(String[] args) {
  3. int[] numbers = {10,20,30,40};
  4. //格式1
  5. for(int i = 0; i < 4; i++) {
  6. System.out.println(numbers[i]);
  7. }
  8. //格式2
  9. for(int x :numbers) {
  10. System.out.println(x);
  11. }
  12. }
  13. }
  1. public class BreakContinue {
  2. public static void main(String[] args) {
  3. for(int i = 0; i < 10; i++) {
  4. if(i == 5) {
  5. continue;
  6. }
  7. if (i == 7) {
  8. break;
  9. }
  10. System.out.print(i);
  11. }
  12. }
  13. }
  1. package circulation;
  2. public class While {
  3. public static void main(String[] args) {
  4. int i = 0;
  5. int sum = 0;
  6. //格式1
  7. while(i <= 100) {
  8. sum += i;
  9. i += 1; //i++
  10. }
  11. //格式2
  12. do {
  13. sum += i;
  14. i += 1;
  15. }while(i <= 100);
  16. System.out.println(sum);
  17. }
  18. }