T02Scanner

来源:互联网 发布:企业网站结构优化 编辑:程序博客网 时间:2024/05/21 18:34

package qsy;

import java.io.File;
import java.util.Scanner;
public class T02Scanner {
 public static void main(String args[]) throws Exception {

  String filename = "data.txt";
  int op1,op2,result=0;
  String operator ="";
  // create a scanner from the data file
  Scanner scanner = new Scanner(new File(filename));

  // 重复从文件中读取数据
  while (scanner.hasNext()) {
  
   operator = scanner.next();
   op1 = scanner.nextInt();
   op2 = scanner.nextInt();
   
   if (operator.equals("+"))
    result = op1 + op2;
   else if (operator.equals("-"))
    result = op1 - op2;
   System.out.println("result is " + result);
  }
  scanner.close(); // also closes the File
 }
}

0 0
原创粉丝点击