动手动脑一:
package cangku; import java.util.Scanner; public class suisishu { public static void main(String[]args){ Scanner in = new Scanner(System.in); System.out.println("请输入数量"); int count=in.nextInt();//输入数量 System.out.println("请输入上限"); int max=in.nextInt();//输入上限 for(int i=0;i<count;i++) {int a=(int)(Math.random()*(max+1)); System.out.println(a); } }}
运行结果如下
动手动脑二:
// MethodOverload.java // Using overloaded methods public class MethodOverload { public static void main(String[] args) { System.out.println("The square of integer 7 is " + square(7)); System.out.println("\nThe square of double 7.5 is " + square(7.5)); } public static int square(int x) { return x * x; } public static double square(double y) { return y * y; } }
运行结果如下:
该函数是典型的函数重载,函数重载需要参数满足三个条件,即参数类型不同,或者参数数量不同,亦或者参数的顺序不同皆可;
课后作业:
package three; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class ReadLine { public static void readline(String file, String writerfile) { try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String str; while ((str = br.readLine()) != null) { System.out.println(str); } br.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { readline("C:\\Users\\凋零\\Desktop\\sd.txt", null); } }
该函数已经能做到导入电脑上的某个文件,接下来就是对数据的分析了。如下截图为导入的电子版的英文小说《飘》: