我有这段代码,我的问题是除非我点击 Enter 键,否则Scanner没有拿起最后一行。
我已经尝试了所有答案,但没有运气。我无法使用BufferedReader
,我必须仅使用Scanner
执行此操作。
Scanner scanner = new Scanner(System.in);
int noOfCases = scanner.nextInt();
scanner.nextLine(); //consuming the left out \n
for (int i = 1; i <= noOfCases; i++) {
int count = 0;
String line = scanner.nextLine();
String arr[] = line.split(" ");
int white = Integer.parseInt(arr[0]);
int red = Integer.parseInt(arr[1]);
int green = Integer.parseInt(arr[2]);
if (white > 0 && red > 0 && green > 0) {
count = count + (white / 3) + (red / 3) + (green / 3);
white = white - ((white / 3) * 3);
red = red - ((red / 3) * 3);
green = green - ((green / 3) * 3);
}
int minVal = Math.min(Math.min(white, red), green);
count = count + minVal;
System.out.println(count);
}
上面的代码没有读取最后一行,传递的输入不在我手中。
示例输入类似于(测试用例后跟测试用例的数量):
3 // no of test cases
5 5 5
10 4 2
4 4 4
答案 0 :(得分:0)
由于某些原因,除非您按Enter键以帮助构建扫描仪,否则不会定义最后一行。 nextInt()
可能就足够了。
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int noOfCases=scanner.nextInt();
scanner.nextLine(); //consuming the left out \n
for(int i=1;i<=noOfCases;i++)
{
int count=0;
int white=scanner.nextInt();
int red=scanner.nextInt();
int green=scanner.nextInt();
if(white>0&&red>0&&green>0)
{
count=count+(white/3)+(red/3)+(green/3);
white=white-((white/3)*3);
red=red-((red/3)*3);
green=green-((green/3)*3);
}
int minVal=Math.min(Math.min(white,red),green);
count=count+minVal;
System.out.println(count);
}
}