我有一個任務的程序,我無法解開最後的消息。它需要能夠接受大量「DNA」代碼片段。給出的樣品在100,000+範圍內。我首先寫了一個小樣本,一行很奇妙的工作。 電訊局長告訴我,我應該可以添加一個while (input.hasNext())
,它將完成比我複製並粘貼到控制檯中的示例文件的第一行更多的內容。當然是這樣!它只是不會結束。我試着用我認爲合適的break;
,但最終回到我所在的位置,只有一條線被計算在內。雖然hasNext()不會結束
Scanner scan = new Scanner(System.in); //Scanner
System.out.println("Enter a DNA sequence consisting of A, T, G, and C, on one line: "); //Instructions for user.
dnaSequence = scan.nextLine(); //Scan for next line of string.
dnaSequence = dnaSequence.toUpperCase(); //Converts all letters entered upper case to avoid issues.
while(scan.hasNext()){
for (int i = 0; i < dnaSequence.length(); i++) //Make i = 0, i has to be less than the length of the entered sequence, will excute count.
{
validCount = dnaSequence.charAt(i); //[FILL IN!]
switch (validCount) //Switch for all valid counts
{
case 'A' : //For any case A.
countA++; //Count all As.
break;
case 'T' : //For any case T.
countT++; //Count all Ts.
break;
case 'C' : //For any case C.
countC++; //Count all Cs.
break;
case 'G' : //For any case G.
countG++; //Count all Gs.
break;
}
}
totalCountGC = countG + countC; //Math for G and C, together.
totalCountSequence = countA + countT + countG + countC; //Math for total count of all other counts in switch.
人們也將認爲它會更適合使用'hasNextLine'的,而不是'hasNext'了'而-loop' – MadProgrammer
我知道這是什麼愚蠢簡單!非常感謝你,我一直在討論java文檔和我的教科書,試圖找出我出錯的地方。非常感激。另外我不知道我可以結合nextline()和upperCase()。 – GoldenDesign