zoukankan      html  css  js  c++  java
  • Java第三次作业

    输入:

     1 import java.util.Scanner;
     2 
     3 public class TextScanner {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         int nextValue, sum = 0;
    11         Scanner kpInput = new Scanner(System.in);//创建Scanner对像
    12         kpInput.useDelimiter("\s");//设置分隔符
    13         while (kpInput.hasNextInt()) {//使用hasNextInt()方法判断是否还有整数
    14             nextValue = kpInput.nextInt();//读取整数流中的下一个整数
    15             sum += nextValue;
    16         }
    17         kpInput.close();
    18         System.out.println("Sum:" + sum);
    19 
    20     }
    21 
    22 }
    
    运算结果:

    输出:
     1 import java.io.File;
     2 import java.io.FileNotFoundException;
     3 import java.util.Scanner;
     4 
     5 
     6 public class Text {
     7     public static void main(String[] args) {
     8         //从文件中逐行读取数据
     9         String name;
    10         int age;
    11         Scanner fileInput;
    12         File inFile = new File("src\Ages.dat");
    13         try {
    14             fileInput = new Scanner(inFile);
    15             while(fileInput.hasNext()){
    16                 name = fileInput.next();
    17                 age = fileInput.nextInt();
    18                 System.out.printf("%s is %d years old.
    ",name,age);
    19             }
    20             fileInput.close();
    21             
    22         } catch (FileNotFoundException e) {
    23             // TODO: handle exception
    24             e.printStackTrace();
    25         }
    26     }
    27 }
    
    

    运算结果:

     
  • 相关阅读:
    css:水平居中和垂直居中
    python celery多worker、多队列、定时任务
    python 单例模式
    单例模式
    django 缓存信号等
    redis常用的命令总结
    在python项目中导出项目依赖的模块信息
    面向对象:类中的特殊成员
    python写算法中的栈
    CatLog_小鱼要加油
  • 原文地址:https://www.cnblogs.com/wawawaawa/p/5330169.html
Copyright © 2011-2022 走看看