我正在尝试一次向该程序输入一些测试用例,它只是继续输出最后一个输入的结果,而没有剩余的输入
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class BeautifulStrings2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//File f=new File("input.txt");
Scanner in=new Scanner(System.in);
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
int n=in.nextInt();
for (int i=1;i<=n;i++){
String s=in1.readLine();
System.out.println(s);
String chars="";
if (Character.isUpperCase(s.charAt(0))){
chars+=s.charAt(0);
}
else {
chars+=Character.toUpperCase(s.charAt(0));
}
for (int j=0;j<s.length();j++){
boolean flag=true;
for (int k=0;k<chars.length();k++){
if (Character.isLetter(s.charAt(j))){
if (chars.charAt(k)==Character.toUpperCase(s.charAt(j))){
flag=false;
break;
}
}
}
if (flag){
chars+=Character.toUpperCase(s.charAt(j));
}
}
String temp=chars;
chars="";
for (int j=0;j<temp.length();j++){
if (temp.charAt(j)>64&&temp.charAt(j)<91)
chars+=temp.charAt(j);
}
int [] numbers=new int[chars.length()];
Arrays.fill(numbers, 0);
for (int j=0;j<chars.length();j++){
for (int k=0;k<s.length();k++){
if (chars.charAt(j)==Character.toUpperCase(s.charAt(k))){
numbers[j]++;
}
}
}
Arrays.sort(numbers);
int factor=26,sum=0;
for (int j=chars.length()-1;j>=0;j--){
sum+=numbers[j]*factor;
factor--;
}
System.out.println("Case #"+i+": "+sum);
}
}
}
样本输入
5
ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves
输出应该是什么
Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646
实际输出
Case #1: 646
编辑 !现在我编辑了我的代码以仅使用 Scanner 对象并添加 in.nextLine() 以跳过第一行
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class BeautifulStrings2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//File f=new File("input.txt");
Scanner in=new Scanner(System.in);
//BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
int n=in.nextInt();
in.nextLine();
for (int i=1;i<=n;i++){
String s=in.nextLine();
System.out.println(s);
String chars="";
if (Character.isUpperCase(s.charAt(0))){
chars+=s.charAt(0);
}
else {
chars+=Character.toUpperCase(s.charAt(0));
}
for (int j=0;j<s.length();j++){
boolean flag=true;
for (int k=0;k<chars.length();k++){
if (Character.isLetter(s.charAt(j))){
if (chars.charAt(k)==Character.toUpperCase(s.charAt(j))){
flag=false;
break;
}
}
}
if (flag){
chars+=Character.toUpperCase(s.charAt(j));
}
}
String temp=chars;
chars="";
for (int j=0;j<temp.length();j++){
if (temp.charAt(j)>64&&temp.charAt(j)<91)
chars+=temp.charAt(j);
}
int [] numbers=new int[chars.length()];
Arrays.fill(numbers, 0);
for (int j=0;j<chars.length();j++){
for (int k=0;k<s.length();k++){
if (chars.charAt(j)==Character.toUpperCase(s.charAt(k))){
numbers[j]++;
}
}
}
System.out.println(chars);
Arrays.sort(numbers);
int factor=26,sum=0;
for (int j=chars.length()-1;j>=0;j--){
sum+=numbers[j]*factor;
factor--;
}
System.out.println("Case #"+i+": "+sum);
}
}
}
输出
Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
在我按回车后出现第五行
Case #5: 646
我怎样才能让它出现而不必按回车?!