⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tokenstandardio.java

📁 中国水利水电出版社java程序设计教程,附源码
💻 JAVA
字号:
//TokenStandardIO.java

import java.io.*;
import java.util.*;

public class TokenStandardIO
{
//需要输入的person数目。
	public static int NUMBER = 3;
	public static void main(String[] args)
	{
		Person[] people=new Person[NUMBER];
//暂时容纳输入数据的临时字符串数组。
		String[] field = new String[4];
//初始化field数组。
		for(int i=0; i < 4; i++)
		{
			field[i] = "";
		}
//IO操作必须捕获IO异常。
		try
		{
//用于对field数组进行增加控制。
			int fieldcount = 0;

//先使用System.in构造InputStreamReader,再构造BufferedReader。
			BufferedReader stdin =
				new BufferedReader(new InputStreamReader(System.in));
			for(int i = 0; i< NUMBER; i++)
			{
				fieldcount = 0;
				System.out.println("The number " + (i + 1) + " person");
				System.out.println("Enter name,age,salary,married(optional), please separate fields by ':'");
//读取一行。
				String personstr=stdin.readLine();
//设置分隔符。
				StringTokenizer st = new StringTokenizer(personstr, ":");
//判断是否还有分隔符可用。		
				while (st.hasMoreTokens())
				{
					field[fieldcount] = st.nextToken();
					fieldcount++;
				}
//如果输入married,则field[3]不为空,调用具有四个参数的Person构造函数。
				if(field[3] != "")
				{
					people[i] = new Person(field[0], Integer.parseInt(field[1]),
						Double.parseDouble(field[2]), field[3]);
//置field[3]为空,以备下次输入使用。
					field[3] = "";
				}
//如果未输入married,则field[3]为空,调用具有三个参数的Person构造函数。
				else
				{
					people[i] = new Person(field[0], Integer.parseInt(field[1]),
						Double.parseDouble(field[2]));
				}
			}
//输出已经输入的数据。
			System.out.println("Output the data of people:");
			for(int i = 0; i < NUMBER; i++)
			{
				System.out.println("The number " + (i + 1) + " person");
				System.out.println("name: " + people[i].getName() + "; age:" + people[i].getAge() +
					"; salary: " + people[i].getSalary() + "; married: " + people[i].getMarried());
			}
		}
		catch (IOException e)
		{
			System.err.println("IOException");
		}
	}
}

class Person
{
	private String name;
	private int age;
	private double salary;
	private String married;

	public Person(String n, int a, double s)
	{
		name = n;
		age = a;
		salary = s;
		married = "F";
	}

	public Person(String n, int a, double s, String m)
	{
		name = n;
		age = a;
		salary = s;
		married = m;
	}

	public String getName()
	{
		return name;
	}

	public int getAge()
	{
		return age;
	}

	public double getSalary()
	{
		return salary;
	}

	public String getMarried()
	{
		return married;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -