Java- 带有包含默认大小写的 switch 语句的 For 循环。如何让默认情况下只打印一次输出?
Java- For loop with a switch statement containing default case. How can I get the default case to print the output only once?
以下默认语句完全按照预期执行:
捕获上述情况中未提及的所有字符,并通知
我(在 运行 之后通过字符串),有一个无效字符
进入那里的某个地方。但是,如果有两个无效字符:
println 语句将打印两次。如果有三个:三次,等等。
在一个 100,000 个字符的字符串中,打印这么多次该行效率很低。
无论有多少无效字符,我怎样才能让它只打印一次
被输入?
请提前告知并感谢您帮助Java个新手!
//for loop to calculate how many A's, G's, T's, and C's in the string
//default statement at the end of the switch statements to weed out
//invalid characters.
for(int i=0; i < length; i++)
{
ch = dna.charAt(i);
switch (ch)
{
case 'A': aCount++;
break;
case 'C': cCount++;
break;
case 'G': gCount++;
break;
case 'T': tCount++;
break;
default:
System.out.println("An invalid character was entered.");
}
}
import java.util.Scanner;
//done by Nadim Baraky
//this program calculates the number of A's, C's, G's & T's;
//it prints a statement once as you wished in case invalid characters where entered
public class ACGT_DNA {
public static void main (String[] args) {
//the counter is set to be 1;
int length, counter=1;
int aCount =0, cCount=0, gCount =0, tCount=0;
char ch;
Scanner scan = new Scanner(System.in);
System.out.print("Enter your string: ");
String dna = scan.next();
scan.close();
length = dna.length();
for(int i=0; i < length; i++) {
ch = dna.charAt(i);
switch (ch) {
case 'A': aCount++;
break;
case 'C': cCount++;
break;
case 'G': gCount++;
break;
case 'T': tCount++;
break;
default:
if(counter==1) {
System.out.println("An invalid character was entered.");
counter++;
//after counter is being incremented, the if statement won't be true; so no matter how invalid characters you enter, the statement will be just be printed once.
}
}
}
System.out.println("A's " + aCount);
System.out.println("C's " + cCount);
System.out.println("G's " + gCount);
System.out.println("T's " + tCount);
}
}
以下默认语句完全按照预期执行: 捕获上述情况中未提及的所有字符,并通知 我(在 运行 之后通过字符串),有一个无效字符 进入那里的某个地方。但是,如果有两个无效字符: println 语句将打印两次。如果有三个:三次,等等。 在一个 100,000 个字符的字符串中,打印这么多次该行效率很低。
无论有多少无效字符,我怎样才能让它只打印一次 被输入? 请提前告知并感谢您帮助Java个新手!
//for loop to calculate how many A's, G's, T's, and C's in the string
//default statement at the end of the switch statements to weed out
//invalid characters.
for(int i=0; i < length; i++)
{
ch = dna.charAt(i);
switch (ch)
{
case 'A': aCount++;
break;
case 'C': cCount++;
break;
case 'G': gCount++;
break;
case 'T': tCount++;
break;
default:
System.out.println("An invalid character was entered.");
}
}
import java.util.Scanner;
//done by Nadim Baraky
//this program calculates the number of A's, C's, G's & T's;
//it prints a statement once as you wished in case invalid characters where entered
public class ACGT_DNA {
public static void main (String[] args) {
//the counter is set to be 1;
int length, counter=1;
int aCount =0, cCount=0, gCount =0, tCount=0;
char ch;
Scanner scan = new Scanner(System.in);
System.out.print("Enter your string: ");
String dna = scan.next();
scan.close();
length = dna.length();
for(int i=0; i < length; i++) {
ch = dna.charAt(i);
switch (ch) {
case 'A': aCount++;
break;
case 'C': cCount++;
break;
case 'G': gCount++;
break;
case 'T': tCount++;
break;
default:
if(counter==1) {
System.out.println("An invalid character was entered.");
counter++;
//after counter is being incremented, the if statement won't be true; so no matter how invalid characters you enter, the statement will be just be printed once.
}
}
}
System.out.println("A's " + aCount);
System.out.println("C's " + cCount);
System.out.println("G's " + gCount);
System.out.println("T's " + tCount);
}
}