课堂小测(二)
不得不说,自己基础是真的差,也是用了两节课和一下午做了这么一个简单的题,文件操作已经全都忘了,下个博客写一篇文件操作。
这是第一次写出来的代码,有很多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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」