1.编写java程序,输入三个整数,并求出三个整数的最大值和最小值。``
package com.lxh.sixteenchapter; import java.util.Scanner; public class InputUtil { private InputUtil() { } private static final Scanner INPUT=new Scanner(System.in); public static int getInt(String prompt) { int num=0; boolean flag=true; while(flag) { System.out.println(prompt); //打印提示信息 if(INPUT.hasNext()) { flag=false; num=INPUT.nextInt(); }else { System.out.println("输入有误"); } } return num; } } package com.lxh.sixteenchapter; public interface INumberSerService { public int [] stat(int count); } package com.lxh.sixteenchapter; public class NumberServiceImpl implements INumberSerService { @Override public int[] stat(int count) { int result[]=new int[2]; int data[]=new int[count]; for(int x=0;x<data.length;x++) { data[x]=InputUtil.getInt("请输入第"+(x+1)+"数字"); } result[0]=data[0]; //最大值 result[1]=data[0]; //最小值 for(int x=0;x<data.length;x++) { if(data[x]>result[0]) { result[0]=data[x]; } if(data[x]<result[0]) { result[1]=data[x]; } } return result; } } package com.lxh.sixteenchapter; public class Factory { private static INumberSerService getInstance() { return new NumberServiceImpl(); } } package com.lxh.sixteenchapter; public class IOCaseDemo { public static void main(String[] args) { INumberService numberService = Factory.getInstance() ; int result [] = numberService.stat(5) ; System.out.println("最大值:" + result[0] + "、最小值:" + result[1]); } }2、 从键盘输入文件的内容和要保存的文件名称,然后根据输入的名称创建文件,并将内容保存到文件中。 (1),定义一个文件操作类的服务接口
public interface IFileService { public boolean save(); }(2)在InputUtil类里面追加有输入字符串的处理方法
package com.lxh.sixteenchapter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class InputUtil02 { private static final BufferedReader INPUT=new BufferedReader(new InputStreamReader(System.in)); private InputUtil02() {} public static String getString(String prompt) { String str = null ; boolean flag = true ; while(flag) { System.out.print(prompt); try { str = INPUT.readLine() ; if (!"".equals(str)) { flag = false ; } else { System.out.println("输入的内容不允许为空!"); } } catch (IOException e) { System.out.println("输入的内容不允许为空!"); } } return str ; } }(3)定义文件操作的业务标准
import java.io.File; public interface IFileServic02 { public static final String SAVE_DIR ="E:"+File.separator+"File"+File.separator+"ll.txt"; public boolean save(); }(4)定义实现子类
package com.lxh.sixteenchapter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; public class FileServiceImpl02 implements IFileServic02 { private String name; private String count; public FileServiceImpl02(String name, String count) { this.name = InputUtil02.getString("请输入保存文件名称"); this.count = InputUtil02.getString("请输入保存文件内容"); } @Override public boolean save() { File file=new File(IFileServic02.SAVE_DIR+this.name); PrintWriter out=null; try { out=new PrintWriter(new FileOutputStream(file)); out.print(this.count); } catch (FileNotFoundException e) { return false; }finally { if(out!=null) { out.close(); } } return true; } }(5)建立工厂类
public class Factory02 { public static IFileServic02 getInstance() { return new FileServiceImpl02(); } }(6)测试
public class Test02 { public static void main(String[] args) { IFileServic02 test02=Factory02.getInstance(); System.out.println(test02.save()); } }执行结果
请输入保存文件名称1232564154841 请输入保存文件内容51651 true