import java.util.Scanner; public class shuzu { public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("请输入数组数目:"); int sum=s.nextInt(); int []a=new int[sum]; System.out.println("请输入"+sum+"个数:"); for(int i=0;i<sum;i++) { a[i]=s.nextInt(); } s.close(); int sum1=0; for(int i=1;i<=sum;i++) { sum1=sum1+i; } int [] b=new int[sum1]; int temp1=0; for(int i=0;i<sum;i++) { int temp=0; for(int k=0;k<sum-i;k++) { temp=temp+a[i+k]; b[temp1]=temp; temp1++; } } for(int i=0;i<sum1-1;i++) for(int k=0;k<sum1-1-i;k++) { if(b[k]<b[k+1]) { int temp2=b[k]; b[k]=b[k+1]; b[k+1]=temp2; } } System.out.println("最大的子数组和为"+b[0]); } }
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class number {
public static void main(String[] args)throws IOException
{
List<Integer> list=new ArrayList<>();
FileWriter fw = null;
try {
//创建字符输出流
fw = new FileWriter("number1.txt");
for(int i=0;i<50;i++)
{
int intval=(int)(Math.random()*-9000000+8999999);
fw.write(""+intval+" ");
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
//使用finally块来关闭文件输出流
if (fw != null) {
fw.close();
}
}
try {
File file = new File("number1.txt");
if(file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = "";
while ((lineTxt = br.readLine()) != null){
list.add(Integer.parseInt(lineTxt));
}
Object[]a=list.toArray();
System.out.println((int)a[1]+(int)a[3]);
br.close();
int sum=a.length;
int sum1=0;
for(int i=1;i<=sum;i++)
{
sum1=sum1+i;
}
int [] b=new int[sum1];
int temp1=0;
for(int i=0;i<sum;i++)
{
int temp=0;
for(int k=0;k<sum-i;k++)
{
temp=temp+(int)a[i+k];
b[temp1]=temp;
temp1++;
}
}
for(int i=0;i<sum1-1;i++)
for(int k=0;k<sum1-1-i;k++)
{
if(b[k]<b[k+1])
{
int temp2=b[k];
b[k]=b[k+1];
b[k+1]=temp2;
}
}
System.out.println("最大的子数组和为"+b[0]);
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件读取错误!");
}
}
public static void main(String[] args)throws IOException
{
List<Integer> list=new ArrayList<>();
FileWriter fw = null;
try {
//创建字符输出流
fw = new FileWriter("number1.txt");
for(int i=0;i<50;i++)
{
int intval=(int)(Math.random()*-9000000+8999999);
fw.write(""+intval+" ");
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
//使用finally块来关闭文件输出流
if (fw != null) {
fw.close();
}
}
try {
File file = new File("number1.txt");
if(file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = "";
while ((lineTxt = br.readLine()) != null){
list.add(Integer.parseInt(lineTxt));
}
Object[]a=list.toArray();
System.out.println((int)a[1]+(int)a[3]);
br.close();
int sum=a.length;
int sum1=0;
for(int i=1;i<=sum;i++)
{
sum1=sum1+i;
}
int [] b=new int[sum1];
int temp1=0;
for(int i=0;i<sum;i++)
{
int temp=0;
for(int k=0;k<sum-i;k++)
{
temp=temp+(int)a[i+k];
b[temp1]=temp;
temp1++;
}
}
for(int i=0;i<sum1-1;i++)
for(int k=0;k<sum1-1-i;k++)
{
if(b[k]<b[k+1])
{
int temp2=b[k];
b[k]=b[k+1];
b[k+1]=temp2;
}
}
System.out.println("最大的子数组和为"+b[0]);
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件读取错误!");
}
}
}
本次实验是对文件的读入读出和程序安全性的练习,本次实验文件读入读出我使用的是file writer和BufferedReader,程序安全性我考虑到文件是否存在和数组数据是否超出内存,我分别使用try catch和throws进行捕获。