java_关于scanner对象的nextInt()方法和nextLine()方法再oj中可能出现的报错问题

是否会报错:处理代码的健壮性,另一方面就是收输入文件中,文件末尾是否有回车符的影响

example

/*
* Input:
有多组数据测试,以输入 0 结束。
第一行输入一个数字 t ,表示接下来有 t 行输入,分别代表 t 只飞龙的飞行方程。
* 每一行的输入格式如下:“3X 2 6 Y 5 3 Z 2”表示飞行方程:“3*x^2+ 6*y^5 + 3 * z ^2”,
* 同样,会存在如下的输入:“1X 3 2 X 3 6 Y 2 3 Z 0”表示飞行方程:“3* x ^ 3 + 6 * y ^ 2 + 3 * z ^ 0”。
* 并保证每行相对应的输出为“a* x ^ (n1) + b * y ^ (n2) + c * z ^ (n3)”,
* 输入的各项系数和次数都为0~ 9的数字,以及输入只会有“X”、“Y”和“Z”三个字母。
* 输入一行不会超过2000个字符长度。
* 输入数据均在int范围内。(若出现幂次为0,则视为1。)
Output:
对于每组测试,输出“Case#k: ”,首先,k从1开始标号。
对于每个飞行方程,输出“a* x ^ (n1) + b * y ^ (n2) + c * z ^(n3)”
* 表示求导后的结果。详细参见样例,每行中会存在空格,注意空格的输出控制。
Sample Input:
*/
/*
2
1 X 3 2 X 3 6 Y 2 3 Z 0
3 X 2 6 Y 5 3 Z 2
4
9 X 9 9 X 9 9 X 9 9 X 9 9 X 9
9 X 9 9 Y 9
9 X 9 9 Z 9
9 Y 9 9 Z 9
0
*/
/*
Sample Output:
Case #1:
9 * X ^ 2 + 12 * Y ^ 1 + 3
6 * X ^ 1 + 30 * Y ^ 4 + 6 * Z ^ 1
Case #2:
405 * X ^ 8
81 * X ^ 8 + 81 * Y ^ 8
81 * X ^ 8 + 81 * Z ^ 8
81 * Y ^ 8 + 81 * Z ^ 8
*
* */
package acmInJava.test_scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
/*simulate the input like the oj :the end of the input file without the enter/return key;*/
File file = new File("/D:\\OneDrive - pop.zjgsu.edu.cn\\javaProject\\src\\acmInJava\\test_scanner/dataIn.txt");
Scanner sc = new Scanner(file);
int count = 0;
while (true) {
int t = sc.nextInt();
/* the error maybe triggered in here :*/
sc.nextLine();
/*if we put the sc.nextLine() out of the if judge,then the the code will throw error
* due to the:java.util.NoSuchElementException: No line found
* well,this depend on the data in the input file:if the file is end with an enter key,the code is still worked;if without enter key ,it will throw runtime error */
/*correct pattern 1:
* if(sc.hasNextLine()){
sc.nextLine();
* }
* */
// System.out.println("numOfEquality="+t);
// System.out.println("test the content:"+sc.nextLine()+"!!!");
/**
* int java.util.Scanner.nextInt() Scans the next token of the input as an int.
*
* An invocation of this method of the form nextInt() behaves in exactly the
* same way as the invocation nextInt(radix), where radix is the default radix
* of this scanner.
*
* Returns: the int scanned from the input Throws: InputMismatchException - if
* the next token does not match the Integer regular expression, or is out of
* range NoSuchElementException - if input is exhausted IllegalStateException -
* if this scanner is closed
*/
if (t == 0) {
break;
}
/**
* String java.util.Scanner.nextLine() Advances(go forward) this scanner past
* the current line and returns the input that was skipped. This method returns
* the rest of the current line, excluding any line separator at the end. The
* position is set to the beginning of the next line.
*
* Since this method continues to search through the input looking for a line
* separator, it may buffer all of the input searching for the line to skip if
* no line separators are present.
*
* Returns: the line that was skipped Throws: NoSuchElementException - if no
* line was found IllegalStateException - if this scanner is closed
*/
// sc.nextLine();// place the sc.nextLine() after the if-break ,the code is accepted
// System.out.println("test the content:"+sc.nextLine()+"!!!");
System.out.println("Case #" + (++count) + ":");
for (int i = 0; i < t; ++i) {
int x_1 = 0, x_exp = 0;
int y_1 = 0, y_exp = 0;
int z_1 = 0, z_exp = 0;
String line = "";
if (sc.hasNextLine()) {
line = sc.nextLine();
}
String[] strs = line.split(" ");
for (int j = 0; j < strs.length; ++j) {
if ("X".equals(strs[j])) {
x_1 += Integer.parseInt(strs[j - 1]);
x_exp = Integer.parseInt((strs[j + 1]));
if (x_exp == 0)
x_exp = 1;
} else if ("Y".equals(strs[j])) {
y_1 += Integer.parseInt(strs[j - 1]);
y_exp = Integer.parseInt((strs[j + 1]));
if (y_exp == 0)
y_exp = 1;
} else if ("Z".equals(strs[j])) {
z_1 += Integer.parseInt(strs[j - 1]);
z_exp = Integer.parseInt((strs[j + 1]));
if (z_exp == 0)
z_exp = 1;
}
}
x_1 *= x_exp;
--x_exp;
y_1 *= y_exp;
--y_exp;
z_1 *= z_exp;
--z_exp;
if (x_1 != 0) {
if (x_exp != 0)
System.out.print(x_1 + " * X ^ " + x_exp);
else
System.out.print(x_1);
if (y_1 != 0 || z_1 != 0)
System.out.print(" + ");
}
if (y_1 != 0) {
if (y_exp != 0)
System.out.print(y_1 + " * Y ^ " + y_exp);
else
System.out.print(y_1);
if (z_1 != 0)
System.out.print(" + ");
}
if (z_1 != 0) {
if (z_exp != 0)
System.out.print(z_1 + " * Z ^ " + z_exp);
else
System.out.print(z_1);
}
System.out.println();
}
}
}
}
posted @   xuchaoxin1375  阅读(13)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-04-30 linux@命令行助手@Man手册补充工具@tldr@example@cheat
点击右上角即可分享
微信分享提示