【发布时间】:2015-08-19 15:50:15
【问题描述】:
好吧,我可能措辞错误,这有点难以解释。所以这里是: 主要目标:通过批处理调用 java 三角形程序,同时还通过管道传输带有输入的 testcase.txt 文件列表,这些文件必须发送到 java 程序以确定三角形类型。无需在 java 程序中硬输入文件名。
问题:不确定如何从批处理命令窗口接受要在 java 应用程序中使用的文件名。
目前,我只使用一个文本文件进行测试,其中包含由空格分隔的 3 个数字。单独运行 java 程序时,我可以键入文本文件的文件路径,一切都按预期工作。但可惜我不能硬编码或询问用户该路径,因为这应该由批处理文件设置,以调用这 15 个测试用例文件发送到程序。我现在不明白的部分代码是这样的:
Scanner input = new Scanner(System.in);
String fileName = input.next();
Scanner reader = new Scanner (new File(fileName));
所以我知道 input.next() 将要求键盘输入如何将其从键盘输入切换到批处理文件输入?如果这是有道理的。
这是我的批处理文件:
@ECHO off
set /P num1="Please enter file path: "
echo You entered %num1%
ECHO Checking for file...
if exist %num1% (
set num1=Congrats! I found the file!
C:\Users\josh\Documents\NetBeansProjects\Triangle\src\TriangleRebuild.java
) else (set num1=File does not exist)
echo %num1%
PAUSE
exit
完整代码:
/*
* Josh
Software Engineering
Structured Triangle Implementation in Java
*/
import java.io.*;
import java.util.*;
public class TriangleRebuild {
public static void main(String[] args) throws FileNotFoundException{
Scanner input = new Scanner(System.in);
String fileName = input.next();
Scanner reader = new Scanner (new File(fileName));
int a;
int b;
int c;
boolean isATriangle;
System.out.println("Enter 3 integers which are sides of a triangle: ");
a = reader.nextInt();
b = reader.nextInt();
c = reader.nextInt();
System.out.println("Side A is: " + a);
System.out.println("Side B is: " + b);
System.out.println("Side C is: " + c);
if((a < b + c) && (b < a + c) && (c < a + b)){
isATriangle = true;
}
else{
isATriangle = false;
}
if(isATriangle){
if((a == b) && (b == c)){
System.out.println("Triangle is Equilateral.");
}
else if((a != b) && (a != c) && (b != c)){
System.out.println("Triangle is Scalene.");
}
else{
System.out.println("Triangle is Isosceles.");
}
}
else{
System.out.println("Not a Triangle.");
}
if((Math.pow(c,2) == Math.pow(a,2) + Math.pow(b,2))){
System.out.println("Triangle is a right Triangle.");
}
else{
System.out.println("Triangle is not a right Triangle.");
}
}
}
【问题讨论】:
标签: java batch-file batch-processing