import java.util.Scanner;
public class demo{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a genome string : ");
String s = input.nextLine();
int index = 0;
int newindex = 0;
boolean flagForFirst = true;
while(true){
index = s.indexOf("ATG",newindex);
if(!(index < s.length() - 2 && index >= 0)){
if(flagForFirst){
System.out.println("no gene is found!");
break;
}
else
break;
}
StringBuilder sb = new StringBuilder();
sb.append(s.charAt(index+3));
sb.append(s.charAt(index+4));
sb.append(s.charAt(index+5));
String stringForGenome = sb.toString();
if(stringForGenome == "TAG" || stringForGenome == "TAA"
|| stringForGenome == "TGA")
break;
else{
while(true){
sb = new StringBuilder();
sb.append(s.charAt(index+3));
sb.append(s.charAt(index+4));
sb.append(s.charAt(index+5));
stringForGenome = sb.toString();
if(stringForGenome.equals("TAG") || stringForGenome.equals("TAA")
|| stringForGenome.equals("TGA"))
break;
else{
System.out.print(stringForGenome);
flagForFirst = false;
}
index = index + 3;
}
}
newindex = index + 3;
System.out.println();
}
}
}
你是指整个串中一个基因结尾串都没有的情况,还是某个基因串没有结尾串的情况?
把提取代码和打印代码分开,有效基因放在一个list里,先不打印,最后时再打印,同时判断有没元素,没有就输出non
如下29-33行的代码加进去就好了
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a genome string : ");
String s = input.nextLine();
int index = 0;
int newindex = 0;
boolean flagForFirst = true;
while(true){
index = s.indexOf("ATG",newindex);
if(!(index < s.length() - 2 && index >= 0)){
if(flagForFirst){
System.out.println("no gene is found!");
break;
}
else
break;
}
StringBuilder sb = new StringBuilder();
sb.append(s.charAt(index+3));
sb.append(s.charAt(index+4));
sb.append(s.charAt(index+5));
String stringForGenome = sb.toString();
if(stringForGenome == "TAG" || stringForGenome == "TAA"
|| stringForGenome == "TGA")
break;
else{
while(true){
//这里加入判断
if(index + 5 >= s.length()) {
System.out.println("no gene is found!");
break;
}
sb = new StringBuilder();
sb.append(s.charAt(index+3));
sb.append(s.charAt(index+4));
sb.append(s.charAt(index+5));
stringForGenome = sb.toString();
if(stringForGenome.equals("TAG") || stringForGenome.equals("TAA")
|| stringForGenome.equals("TGA"))
break;
else{
System.out.print(stringForGenome);
flagForFirst = false;
}
index = index + 3;
}
}
newindex = index + 3;
System.out.println();
}
}
测试截图:
代码实现如下:
import java.util.Date;
import java.util.Scanner;
import javax.swing.*;
public class demo{
//找出基因
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a genome string : ");
String s = input.nextLine();
int index = 0;
int newindex = 0;
boolean flagForFirst = true;//是否有基因存在判断标志
//基因存在判断
while(true){
index = s.indexOf("ATG",newindex);
//如果找不到或超出字符串范围结束判断
if(!(index < s.length() - 2 && index >= 0)){
if(flagForFirst){
System.out.println("no gene is found!");
break;
}
else
break;
}
//将三个字符组合成基因
StringBuilder sb = new StringBuilder();
sb.append(s.charAt(index+3));
sb.append(s.charAt(index+4));
sb.append(s.charAt(index+5));
String stringForGenome = sb.toString();
//是否是结束基因
if(stringForGenome == "TAG" || stringForGenome == "TAA"
|| stringForGenome == "TGA")
break;
else{
while(true){
sb = new StringBuilder();
sb.append(s.charAt(index+3));
sb.append(s.charAt(index+4));
sb.append(s.charAt(index+5));
stringForGenome = sb.toString();
if(stringForGenome.equals("TAG") || stringForGenome.equals("TAA")
|| stringForGenome.equals("TGA"))
break;
else{
System.out.print(stringForGenome);
flagForFirst = false;
}
index = index + 3;//继续判断后三位字符是否是基因
}
}
newindex = index + 3;//从新的基因点出发继续判断
System.out.println();//第一段基因结束,换行
}
}
}
如有帮助,还请采纳!谢谢!
天哪, 字符串不要用 == , 用 equals, 或者equals忽略大小写的那个方法, 你有用到equals, 但是第24行为什么用==呢?
题目“没有找到基因”的情况是不是只有这三种呢: 情况一: ATG,,,,,,,,,,TAG 情况二: ATG,,,,,,,,,,TAA 情况三: ATG,,,,,,,,,,TGA, 这个很好判断啊, String的 startsWith和endsWith
把提取代码和打印代码分开