Java 将我的代码从从从键盘读取更改为从文本文件读取
我有一个Java程序,可以找到最小的可能的矩形“栅栏”,以在不同的坐标系下围住绵羊。现在,程序从键盘读取数据。但我需要它从文本文件中读取。这就是文本文件的内容:Java 将我的代码从从从键盘读取更改为从文本文件读取,java,Java,我有一个Java程序,可以找到最小的可能的矩形“栅栏”,以在不同的坐标系下围住绵羊。现在,程序从键盘读取数据。但我需要它从文本文件中读取。这就是文本文件的内容: 5 0 0 0 5 10 5 3 3 10 0 这是我目前的代码: import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class BSheep { final static int MIN
5
0 0
0 5
10 5
3 3
10 0
这是我目前的代码:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class BSheep {
final static int MIN_SHEEP = 2;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nSheep = sc.nextInt();
// validate nSheep
if (nSheep < MIN_SHEEP) {
System.out.println("Invalid input! We need at least " + MIN_SHEEP + " sheep to build a fence!");
System.exit(1);
}
int xMin, xMax, yMin, yMax;
// reading the 1st coordinate, using it to initialize min, max values
xMin = xMax = sc.nextInt();
yMin = yMax = sc.nextInt();
// read the remaining coordinates
for (int i = 1; i <= nSheep - 1; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
System.out.print("Fence Coordinates: {(" + xMin + "," + yMin + "), ");
System.out.print("(" + xMax + "," + yMin + "), ");
System.out.print("(" + xMax + "," + yMax + "), ");
System.out.println("(" + xMin + "," + yMax + ") ");
} // end of main function
} // end of class definition
import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
公共类BSheep{
最终静态积分最小值=2;
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int nSheep=sc.nextInt();
//验证nSheep
如果(n睡眠yMax)
yMax=y;
}
打印(“栅栏坐标:{(“+xMin+”,“+yMin+”)”);
系统输出打印(“+xMax+”、“+yMin+”);
系统输出打印(“+xMax+”、“+yMax+”);
System.out.println(“(“+xMin+”,“+yMax+”);
}//主函数结束
}//类定义结束
那怎么办
java BSheep