黑马程序员技术交流社区

标题: 看了段代码不知道错哪里了,求大神帮助!! [打印本页]

作者: joeywr    时间: 2015-8-21 22:14
标题: 看了段代码不知道错哪里了,求大神帮助!!
import java.util.Scanner;

public class Demo2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String[] sp = line.split(" ");
if (sp[1] == "?") {
sp[1] = "\\?";
}
// System.out.println(sp[1]);
String[] sp1 = sp[0].split(sp[1]);
int size = Integer.parseInt(sp[sp.length - 1]);
if (size > sp1.length) {
System.out.println(-1);
return;
} else {
System.out.println(sp1[Integer.parseInt(sp[sp.length - 1]) - 1]);
}
}
}
样例输入:AAA?BBB?CCC? ? 2
输出:BBB



上面的代码错在哪里?
作者: joeywr    时间: 2015-8-22 10:59
。。。没有人,搞了半天原用来字符串要用.equals!
import java.util.Scanner;

public class Demo2 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Scanner sc = new Scanner(System.in);
                String line = sc.nextLine();
                String[] sp = line.split(" ");
                if (sp[1].equals("?") || sp[1].equals("+")) {
                        sp[1] = "\\" + sp[1];
                }
                System.out.println(sp[1]);
                String[] sp1 = sp[0].split(sp[1]);
                int size = Integer.parseInt(sp[sp.length - 1]);
                if (size > sp1.length) {
                        System.out.println(-1);
                        return;
                } else {
                        System.out.println(sp1[Integer.parseInt(sp[sp.length - 1]) - 1]);
                }
        }
}                                  成功!!!

作者: liuch111    时间: 2015-8-22 12:22
你这题 需求到底是什么
没看懂




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2