我在解决 USACO 培训页面上的第一个问题时遇到了麻烦。
该任务要求从 ride.in
文件中获取两个字符串,将字符串转换为字母乘积的数字(其中 a=1、b=2、z=26 ),然后查看数字的余数是否 = 47(如果是,则打印“GO”,如果不是,则在 ride.out
上打印“STAY”)。
我不知道为什么无法在 ride.out
上打印输出。
我写的代码是:
package ashish.usaco.com;
import java.io.*;
import java.util.*;
class ride {
public static void main(String[] args) throws IOException {
try {
BufferedReader input = new BufferedReader(new FileReader("ride.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ride.out")));
// Use StringTokenizer vs. readLine/split -- lots faster
StringTokenizer st = new StringTokenizer(input.readLine());
if (toNumber(st.nextToken()) % 47 == toNumber(st.nextToken()) % 47) {
out.println("GO");
} else {
out.println("STAY");
}
out.close();
input.close();
System.exit(0);
} catch(NoSuchElementException e) {
System.out.println("File not Found");
} catch(IOException e) {
System.out.println("File not Found");
}
}
public static int toNumber (String name) {
int pdtvalue =1, charvalue =1;
for(int i=0; i<name.length();i++)
{
charvalue = (name.charAt(i) - 'A')+1;
pdtvalue*= charvalue;
}
return pdtvalue;
}
}
最佳答案
感谢大家的帮助..
特别感谢alobodkz..你确实讲了正确的部分。我的ride.java程序找不到ride.in文件,因为它不在正确的目录中。我删除了包含代码的整个包并编写了一个新的ride.java类文件,这次没有 包声明..
关于java - 无法在 "ride.out"文件上打印我的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24936836/