Software engineering second week of personal work

package text;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class three {
    
    public static int length=0;
    
    public static int[] readArray() {
        int[] a = new int[100];
        int t;
        String num="";
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\AZ\\Desktop\\Array.txt"));
            while ((t = br.read()) != -1) {
                if(t==45 && num=="") {    
                    num="-";
                    continue;
                }
                if(t>=48 && t<=57)   
                    num += (char)t;
                if((t<48 || t>57) && num!="" && num!="-") {  
                    a[length++]=Integer.parseInt(num);
                    if(t==45)        
                        num="-";
                    else
                        num="";
                }
            }
            if(!(num=="" || num=="-"))  
                a[length++]=Integer.parseInt(num);            
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println ( "file does not exist!" );
        } catch (IOException e) {
            System.out.println ( "reading file an exception!" );
        } 
        return a;
    }
    
    public static void maxnumber(int[] a) {
        int[] b = new int[100]; 
        b[0] = a[0];
        int j=0,max=0,maxnum=0;
        for(int i=1;i<length;i++) {
            if(a[i]<0 && b[j]>0)
                b[++j] = a[i];
            else if(a[i]>0 && b[j]<0)
                b[++j] = a[i];
            else if(a[i]<=0 && b[j]<0)
                b[j] += a[i];
            else if(a[i]>=0 && b[j]>0)
                b[j] += a[i];
        }
       
        if(b[0]<=0 && b[1]==0) {    
            max=a[0];
            for(int i=1;i<10;i++)
                if(max<a[i])
                    max=a[i];
        } 
        else {
            max=b[0];
            for(int i=0;i<=j;i++)
                if(max<b[i]) {
                    max=b[i];
                    maxnum=i;
                }
            int t=0;
            for(int i=maxnum;i<=j-2;i+=2) {    
                t=t+b[i+1]+b[i+2];        
                if(t>0) {
                    max+=t;
                    t=0;
                }
            }
            t=0;
            for(int i=maxnum;i>=2;i-=2) {   
                t=t+b[i-1]+b[i-2];        
                if(t>0) {
                    max+=t;
                    t=0;
                }
            }
        }    
        System.out.println ( "sub-array and the maximum is:" + max);
    }
    public static void main(String[] args) {
        maxnumber(readArray());
    }
}

 

 

 

Can do is read the file, but the robustness of the program is not guaranteed.

 

Guess you like

Origin www.cnblogs.com/vvxvv/p/12378200.html