如果使用者輸入一串字串(EX:ATCGGTACATC)
凡是A的我都要轉成T
T的轉成A,C轉成G,G轉成C
那我該如何寫呢??
我想要的功能就是
1.使用者輸入
2.馬上就轉換
3.輸出轉換後的字串
2007-10-09 16:07:35 · 3 個解答 · 發問者 維尼 1 in 電腦與網際網路 ➔ 程式設計
public class A {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);//宣告能讀入字串的物件
System.out.print("使用者輸入 : ");
String source = scanner.nextLine();//讀入字串
char[] datas = source.toCharArray();
//轉換開始
for (int i = 0; i < datas.length; i++) {
//凡是A的我都要轉成T,T的轉成A,C轉成G,G轉成C
switch (datas[i]) {
case 'A':
datas[i] = 'T';
break;
case 'T':
datas[i] = 'A';
break;
case 'C':
datas[i] = 'G';
break;
case 'G':
datas[i] = 'C';
break;
}
}
//輸出轉換後的字串
System.out.println("轉換後的字串 : "+String.valueOf(datas));
}
}
2007-10-12 06:36:01 · answer #1 · answered by 忘記人 3 · 0⤊ 0⤋
import java.util.*;
public class In{
public static void main(String[] args){
Scanner rd = new Scanner(System.in);//宣告能讀入字串的物件
String in = rd.next();//從rd.next()讀入的字串...割成w陣列...
String[] w = in.split("");//割成的w陣列...
String finished = "";//完成轉換後,放在 finished
for(int i=0;i<=in.length();i ){//比對轉換迴圈開始
if(w[i].equals("A")){
finished ="T";
}else if(w[i].equals("T")){
finished ="A";
}else if(w[i].equals("C")){
finished ="G";
}else if(w[i].equals("G")){
finished ="C";
}
}//比對轉換迴圈結束
System.out.println(finished);//輸出轉換後的字串
}
}
2007-10-09 20:35:40 · answer #2 · answered by Anonymous · 0⤊ 0⤋
import java.io.*;
//import java.util.*;
public class TEST
//檔名:TEST.java
{
public static void main(String[] args)
{
PrintStream o=new PrintStream(System.out);
//Scanner i=new Scanner(System.in);
String str="ATCGGTACATC";
char chStr[]=str.toCharArray();
int i;
for(i=0;i
if(str.charAt(i)=='A')
{
chStr[i]='T';
}
if(str.charAt(i)=='T')
{
chStr[i]='A';
}
if(str.charAt(i)=='C')
{
chStr[i]='G';
}
if(str.charAt(i)=='G')
{
chStr[i]='C';
}
}
str=String.copyValueOf(chStr);
o.printf("%s",str);
}
}
2007-10-09 18:11:19 · answer #3 · answered by Big_John-tw 7 · 0⤊ 0⤋