0

所以我很确定我错误地使用了 next 和 hasNext... 我正在尝试输入一串 ACGT 字符,然后计算各个结果字母。提前致谢。

import java.util.Scanner;

public class A1Novice {
    public static void main(String[] args){
        String input = "";
        Scanner s = new Scanner(System.in);
        Scanner j = new Scanner(input);
        System.out.println("Enter nucleobases (enter end when done)");
        while(true){
            input = s.next() + input;
            if(s.next().contains("end")){
                break;
            }
        }
        process(j, input);  
    }

    public static void process(Scanner j, String input){
        int a = 0, c = 0, g = 0, t = 0;
        while(j.hasNext()){
            if (j.next()=="A"){
                a++;
            }
            else if(j.next()=="C"){
                c++;
            }
            else if(j.next()=="G"){
                g++;
            }
            else if(j.next()=="T"){
                t++;
            }
            else{
                System.out.println("A count: " + a);
                System.out.println("C count: " + c);
                System.out.println("G count: " + g);
                System.out.println("T count: " + t);
                break;
                }
         }

    }
}
4

4 回答 4

2

把它放在一个局部变量中。

 String next = j.next();

并在您的 if 语句中使用它。否则,您将在每次检查中推进迭代器。

这是它的样子:

public static void process(Scanner j, String input){
    int a = 0, c = 0, g = 0, t = 0;

    while(j.hasNext()){
        String n = j.next();
        if (n.equals("A")){
            a++;
        }
        else if(n.equals("C")){
            c++;
        }
        else if(n.equals("G")){
            g++;
        }
        else if(n.equals("T")){
            t++;
        }
        else{
            ...
        }
     }

}

另外,不要使用 == 来比较字符串。== 运算符将检查它是否是同一个对象实例,但不检查内容是否相等。请改用该equals方法。

于 2014-01-19T18:23:06.180 回答
2

如前所述,您必须将下一个元素放在局部变量中。

然而,这还不够:请永远不要==用来比较字符串。改用 String 的方法equals,甚至equalsIgnoreCase.

你应该使用类似的东西:

String val = j.next();
if(val.equals("A")){ // you could use val.equalsIgnoreCase("A") to be case insensitive
    ...
} else if (val.equals("B")){
    ...

正如 aljipa 的回答所建议的那样,您可以使用开关。如果您希望您的测试不区分大小写,也可以编写如下内容:

String val = j.next();
switch (val.toUpperCase()) {
    case "A":
        a++;
        break;
    case "B":
        ...
于 2014-01-19T18:30:16.810 回答
1

k-mera 的答案是正确的,但考虑使用 switch,因为它在无症状方面更有效:

public static void process( Scanner j, String input ) {
 int a = 0, c = 0, g = 0, t = 0;
 String ch;
 while( j.hasNext() ) {
     switch( ch = j.next() ) {
         case "A":
              a++;
              break;
     case "C":
          c++;
          break;
     case "G":
          g++;
          break;
     case "T":
          t++;
          break;
         default:
              System.out.println("A count: " + a);
              System.out.println("C count: " + c);
              System.out.println("G count: " + g);
              System.out.println("T count: " + t);
              return;
         }
     }
}

此外,如果您需要逐个字符地读取字符,我宁愿使用简单的(字符) System.in.read() t 以自定义方式标记输入。

于 2014-01-19T18:30:09.370 回答
0

迭代器上的 j.next() 基本上会继续下一个循环,因此您在每个循环上进行 4 个循环。

于 2014-01-19T18:24:14.997 回答