案例
编写程序,输入3个整数并求出最大值和最小值(输入错误可以冲重新输入)
class InputUtil {
private InputUtil(){}
/**
* 实现键盘接受数字的操作(输错3次则结束)
* @param prompt 提示信息
* @return 一个可以使用的数字
* @throws IllegalArgumentException 输错3次返回异常
*/
public static int getInt(String prompt,Object... args)throws IllegalArgumentException{
int i=0;
Scanner input;
while (i<3){
System.out.printf(prompt,args);
input=new Scanner(System.in);
if(input.hasNextInt()){
return input.nextInt();
}else{
i++;
System.out.printf("输入的内容不是数字,您还有%s次机会\n",3-i);
}
}
throw new IllegalArgumentException("连续3次输错,程序停止");
}
}
interface INumberService{
/**
* 输入数据并返回输入数据的最大值和最小值
* @param count 表示要输入数据的个数
* @return 包含两个内容:[0]为最大值,[1]为最小值
*/
int[] stat(int count);
}
class NumberServiceImpl implements INumberService{
@Override
public int[] stat(int count) {
int[] result=new int[2];
int data[]=new int[count];
for (int i = 0; i < count; i++) {//信息的循环输入
data[i]=InputUtil.getInt("请输入第%s个数字:",i+1);
}
Arrays.sort(data);
result[0]=data[data.length-1];
result[1]=data[0];
return result;
}
}
class Factory{
private Factory(){}
public static INumberService getInstance(){
return new NumberServiceImpl();
}
}
public class IOCaseDemo{
public static void main(String[] args) {
INumberService numberService=Factory.getInstance();
int[] nums=numberService.stat(5);
System.out.printf("最大值:%s、最小值:%s\n",nums[0],nums[1]);
}
}
案例
用键盘输入文件的内容和要保存的文件名称,然后根据输入的名称创建文件,并将内容保存得到文件中。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class IOCaseDemo {
static {
File dir = new File(IFileService.SAVE_DIR);
if (dir.exists() == false) {
dir.mkdirs();
}
}
public static void main(String[] args) {
IFileService service= Factory.getInstance();
boolean rs=service.save();
System.out.println(rs?"文件保存成功!":"文件保存失败!");
}
}
class Factory{
private Factory(){}
public static IFileService getInstance(){
return new FileServiceImpl();
}
}
interface IFileService {
String SAVE_DIR = "/Users/fzc/Documents/mydir";
/**
* 定义文件的保存处理方法
*
* @return 保存成功为true,保存失败返回false
*/
boolean save();
}
class FileServiceImpl implements IFileService {
private String name;
private String content;
public FileServiceImpl() {
this.name = InputUtil.getStr("请输入文件名称:");
this.content = InputUtil.getStr("请输入文件的内容:");
}
@Override
public boolean save() {
File file = new File(new File(SAVE_DIR), name);
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileOutputStream(file));
writer.print(content);
} catch (FileNotFoundException e) {
return false;
} finally {
if (writer != null) {
writer.close();
}
}
return true;
}
}
class InputUtil {
private InputUtil() {
}
/**
* 实现键盘接收字符串的操作(输错3次则结束)
*
* @param prompt 提示信息
* @return 输入的字符串
* @throws IllegalArgumentException 输错3次返回异常
*/
public static String getStr(String prompt, Object... args) throws IllegalArgumentException {
int i = 0;
String str;
Scanner input;
while (i < 3) {
System.out.printf(prompt, args);
input = new Scanner(System.in);
if (input.hasNext()) {
str = input.next();
if ("".equals(str.trim())) {
i++;
System.out.printf("输入的内容不允许为空,您还有%s次机会\n", 3 - i);
}
return str;
} else {
i++;
System.out.printf("未获取到你输入的内容,您还有%s次机会\n", 3 - i);
}
}
throw new IllegalArgumentException("连续3次输错,程序停止");
}
}
案例
从键盘多次输入多个字符串到程序中,并逆序输出(使用StringBuffer)。
import java.util.Arrays;
import java.util.Scanner;
interface IStringService {
void append(String s);//追加字符串
String[] reverse();//反转处理
}
public class IOCaseDemo {
public static void main(String[] args) {
new Menu();
}
}
class Menu {
private IStringService stringService;
public Menu() {
stringService = Factory.getInstance();
choose();
}
public void choose() {
this.show();
String choose = InputUtil.getStr("请进行选择");
switch (choose) {
case "1":
String str = InputUtil.getStr("请输入字符串数据:");
stringService.append(str);
choose();
break;
case "2":
String[] res = stringService.reverse();
System.out.println(Arrays.toString(res));
choose();
break;
case "0":
System.out.println("下次再见,bye");
System.exit(1);
default:
System.out.println("你选择的序号不正确,请重新输入");
choose();
}
}
public void show() {
System.out.println("【1】追加字符串数据\n");
System.out.println("【2】逆序显示所有字符串数据\n");
System.out.println("【0】结束程序执行\n\n\n");
}
}
class Factory {
private Factory() {
}
public static IStringService getInstance() {
return new StringServiceImpl();
}
}
class StringServiceImpl implements IStringService {
private StringBuffer data = new StringBuffer();
@Override
public void append(String s) {
this.data.append(s).append("|");
}
@Override
public String[] reverse() {
String result[] = data.toString().split("\\|");
int center = result.length / 2;
int head = 0;
int tail = result.length - 1;
for (int i = 0; i < center; i++) {
String tmp = result[head];
result[head] = result[tail];
result[tail] = tmp;
}
return result;
}
}
class InputUtil {
private InputUtil() {
}
/**
* 实现键盘接收字符串的操作(输错3次则结束)
*
* @param prompt 提示信息
* @return 输入的字符串
* @throws IllegalArgumentException 输错3次返回异常
*/
public static String getStr(String prompt, Object... args) throws IllegalArgumentException {
int i = 0;
String str;
Scanner input;
while (i < 3) {
System.out.printf(prompt, args);
input = new Scanner(System.in);
if (input.hasNext()) {
str = input.next();
if ("".equals(str.trim())) {
i++;
System.out.printf("输入的内容不允许为空,您还有%s次机会\n", 3 - i);
}
return str;
} else {
i++;
System.out.printf("未获取到你输入的内容,您还有%s次机会\n", 3 - i);
}
}
throw new IllegalArgumentException("连续3次输错,程序停止");
}
}