课堂小测(二)

不得不说,自己基础是真的差,也是用了两节课和一下午做了这么一个简单的题,文件操作已经全都忘了,下个博客写一篇文件操作。

 

 

 这是第一次写出来的代码,有很多bug,比如当遇到双位数时,当输入的数长度太大时,输入不规范字符时....... ,明天将进行改正。今天学的脑袋疼

import java.util.*;
import java.io.*;
public class two_3 {

static Scanner scanner = new Scanner(System.in);
static File file = new File("D:/input.txt");
static String result = new String();//储存读取的内容

public static void main(String[] args) throws IOException {


if (file.exists())
{ // 检查input.txt是否存在
System.out.println("File already exists");

}
else
{
System.out.println("File don't exists");

}






FileInputStream f1= new FileInputStream(file);


int length=0;
int length_2=0 ;


//将数字和符号储存在String中
for(int i=0;i<file.length();i++) {
char ch=(char)(f1.read());//循环读取字符
if(ch!='\r' &&ch!='\n') {
if(ch!=',' && ((ch>='0'&& ch<='9') || ch=='-')) {
result+=ch;
}
//补丁节点1
else if (ch!=',' && ((ch<='0'|| ch>='9') || ch!='-')) {
System.out.print("数据格式有误");
System.exit(1);
}
else {
result+=" ";
}

}
}



System.out.println(result);





















char c = result.charAt(0);
length=(int)c-48;
char d = result.charAt(2);
length_2=(int)d-48;

// for(int o=0;o<17;o++)
// System.out.println("***********"+result.charAt(o));

int tempInt[][] = new int[length][length_2];

int max = 0;
int tempMax = 0;//当前最大值
int n = 4;
int flag='-'-48;

for (int i = 0; i < length; i++) {
for(int j=0;j<length_2;j++) {
tempInt[i][j] =result.charAt(n)-48;//一个元素一个元素的输入

if(tempInt[i][j]==flag) {
n=n+1;

tempInt[i][j]=0- (result.charAt(n)-48);//为啥减去59,还没有想明白,多次试验得出的结果。


}

if (i==0&&j == 0) {//第一个是元素
tempMax = tempInt[i][j];
max = tempInt[i][j];
} else {
if (tempMax < 0) {
tempMax = tempInt[i][j];
} else {
tempMax += tempInt[i][j];
}
}

if (tempMax > max) {
max = tempMax;
}
n=n+2;

}

}
System.out.println("结果是:*********");
System.out.println(max);
}

}

 

  

posted @   信2005-2赵磊  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示