码迷,mamicode.com
首页 > 编程语言 > 详细

数组(续)

时间:2020-02-27 19:23:38      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:||   图片   NPU   flush   row   trace   ack   write   txt   

技术图片

 

 

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Test3 {
    static String s="";
    static Scanner cin = new Scanner(System.in);
    
    public static String read() throws IOException{
        
        
        FileInputStream fis = new FileInputStream("E:\\shu1.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String temp = "";
        while((temp = br.readLine()) != null) 
            s = s + temp + "\n";  
                     
            fis.close();
        br.close();
                        
        return s;
    }
    
    public static void write() throws IOException{
        int n;
        System.out.println("请输入计算的数据量");
        boolean continueInput=true;
        do {                  //输入的如果不是整数,报错。
        try {
                n=cin.nextInt();
                continueInput=false;
                File file1 = new File("E:\\shu1.txt");
                FileWriter out = new FileWriter(file1);
                BufferedWriter bw = new BufferedWriter(out);
                for(int i = 0;i < n ; i++){
                       
                    int ran = (int)(Math.random()*10000000) - (int)(Math.random()*20000000);
                    BigInteger a = new BigInteger(String.valueOf(ran));
                    bw.write(String.valueOf(a) + " ");
                
                }
                bw.close();
        }catch(InputMismatchException e) {
         
            System.out.println("try again(incorrect input:an integer is required");
        cin.nextLine();
        }
        }while(continueInput);
        
      
      /*  File file1 = new File("E:\\shu1.txt");
        FileWriter out = new FileWriter(file1);
        BufferedWriter bw = new BufferedWriter(out);
        for(int i = 0;i < n ; i++){
               
            int ran = (int)(Math.random()*10000000) - (int)(Math.random()*20000000);
            BigInteger a = new BigInteger(String.valueOf(ran));
            bw.write(String.valueOf(a) + " ");
        
        }
        bw.close();
       */
            
    }
    public static void clearInfoForFile(String fileName) {
        File file =new File(fileName);
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        clearInfoForFile("E:\\shu1.txt");
        write();
        read();
        String[] num = s.split("[^(-9--10-9)]");
        BigInteger value = new BigInteger("0");
        BigInteger sum = new BigInteger(String.valueOf(num[0]));  
        for(int i = 0;i < num.length;i++){
            int a = value.compareTo(BigInteger.valueOf(0));
            if ( a == 0 || a == -1) {       
                value = new BigInteger(String.valueOf(num[i]));   
            }else {
                   value.add(new BigInteger(String.valueOf(num[i])));      
                  }      
            if (sum.compareTo(value) == -1) {  
                       sum = value;
                   }
               }
                
            System.out.println("最大值为:" + sum);
        
    }
          
    
    
    
    

 

数组(续)

标签:||   图片   NPU   flush   row   trace   ack   write   txt   

原文地址:https://www.cnblogs.com/wendi/p/12373545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!