这种语法可行吗? [英] Is this syntax possible?
问题描述
这种语法是否可行?
for(int counter = o,charArray [0]; counter< = 10,charArray []< = 0; counter ++, charArray [] ++)
我问这个问题,因为我正在努力学习我正在编写的代码。我正在使用字符串搜索代码,我希望将其用于包含以下内容的以下文本文件:
KA-MA-NA-PA-QA
AA -DA-IA-MA
AA-BA-DA-EA-FA-HA
DA-EA-IA-MA-NA
EA-GA-HA-JA-PA-QA
BA-GA-IA-MA-LA
BA-EA-IA-KA-LA-PA >
AA-FA-JA-KA-LA
CA-GA-HA-LA-MA
CA-EA-FA-GA-LA-PA
BA-CA-JA-MA
BA-DA-GA-HA-IA-KA
AA-FA-MA-OA- PA
CA-FA-HA-IA-JA-PA
AA-JA-LA-NA-QA
AA-BA-CA -IA-NA-QA
IA-JA-MA-PA
CA-DA-FA-GA-HA-QA
HA- IA-KA-LA-NA
FA-MA-OA-PA
EA-GA-LA-OA-QA
BA-EA -GA-IA-QA
AA-HA-IA-MA-PA
CA-DA-KA-OA-PA
我对源代码的意图是它应该只搜索文件中的12行,而不是全部24行,以便重复出现任何字符。从我所做的,代码遍历整个列表12次,而不是仅通过12行进行一个循环。我相信只有一个for循环可以实现我想要的动作 - 虽然我不知道代码应放在哪里。顺便说一句,我包括下面的代码:
import java.io. *;
import java.util。*;
public class stringsearch {
public static void main(String args [])throws IOException {
Scanner search = new Scanner(new File(stringtext.txt));
int counterA = 0;
int counterB = 0;
int counterC = 0;
int counterD = 0;
int counterE = 0;
int counterF = 0;
int counterG = 0;
int counterH = 0;
int counterI = 0;
int counterJ = 0;
int counterK = 0;
int counterL = 0;
int counterM = 0;
int counterN = 0;
int counterO = 0;
int counterP = 0;
int counterQ = 0;
int arrayindex = 0;
while(search.hasNextLine())
{// while loop starting brace
String scanline = search.nextLine();
String [] charArray = scanline.split( - );
for(int counter = 0; counter< = 42; counter ++)
{// for loops starting brace
if(charArray [arrayindex] .equals(AA))
{//第一个主外部if循环起始大括号
counterA ++;
if(counterA> = 3){
System.out.println(AA非常常见);
} else {
System.out.println(AA是最不常见的);
}
} //第一个主要外部if循环结束括号
else if(charArray [arrayindex] .equals(BA))
{//第二个主要外部if循环起始支撑
counterB ++;
if(cluster_B_counter> = 3){
System.out.println(BA非常常见);
} else {
System.out.println(BA很常见);
}
} //第二个主外部if循环结束大括号
} // for循环结束大括号
} // while循环结束大括号
}
}
您对我如何使代码按照我想要的方式工作有什么建议吗?
这是一个经典的家庭作业 - 所以我添加了标签。
但是你提供了代码 - > +5就是这样!
你正在使用一个while循环 - 一个运行直到列表完成。
所以只需切换一个到一个for循环来运行所需的12行。
或者添加一个计数器并计算你经过的行数。
还有另一个for循环:
for ( int counter = 0; counter< = 42; counter ++){
// 花哨的代码
}
为什么那个数到42?
Is this syntax possible?
for (int counter=o,charArray[0]; counter <=10,charArray[] <=0; counter++, charArray[]++)
I am asking this since I am having a hard time with the code I'm making. I'mplaying around with a string searching code and I want to use that for the following text file with this content:
KA-MA-NA-PA-QA
AA-DA-IA-MA
AA-BA-DA-EA-FA-HA
DA-EA-IA-MA-NA
EA-GA-HA-JA-PA-QA
BA-GA-IA-MA-LA
BA-EA-IA-KA-LA-PA
AA-FA-JA-KA-LA
CA-GA-HA-LA-MA
CA-EA-FA-GA-LA-PA
BA-CA-JA-MA
BA-DA-GA-HA-IA-KA
AA-FA-MA-OA-PA
CA-FA-HA-IA-JA-PA
AA-JA-LA-NA-QA
AA-BA-CA-IA-NA-QA
IA-JA-MA-PA
CA-DA-FA-GA-HA-QA
HA-IA-KA-LA-NA
FA-MA-OA-PA
EA-GA-LA-OA-QA
BA-EA-GA-IA-QA
AA-HA-IA-MA-PA
CA-DA-KA-OA-PA
What I intended for the source code I made is that it should search through only 12 lines in the file, instead of all 24 lines, for any repeating occurrence of a character. From what I did, the code goes through the entire list 12 times instead of going through 12 lines for one loop only. I believed that only a "for" loop can achieve the action I want - though I don't know where in the code I should place it. By the way, I included the code below:
import java.io.*; import java.util.*; public class stringsearch{ public static void main (String args [])throws IOException { Scanner search = new Scanner (new File ("stringtext.txt")); int counterA=0; int counterB=0; int counterC=0; int counterD=0; int counterE=0; int counterF=0; int counterG=0; int counterH=0; int counterI=0; int counterJ=0; int counterK=0; int counterL=0; int counterM=0; int counterN=0; int counterO=0; int counterP=0; int counterQ=0; int arrayindex=0; while (search.hasNextLine()) { //while loop starting brace String scanline = search.nextLine(); String [] charArray = scanline.split("-"); for (int counter=0; counter<=42; counter++) { // for loop starting brace if (charArray[arrayindex].equals("AA")) { //first main outer if loop starting brace counterA++; if (counterA>=3) { System.out.println ("AA is very common"); } else { System.out.println ("AA is least common"); } } //first main outer if loop ending brace else if (charArray[arrayindex].equals("BA")) { //second main outer if loop starting brace counterB++; if (cluster_B_counter>=3) { System.out.println ("BA is very common"); } else { System.out.println ("BA is very common"); } } //second main outer if loop ending brace } // for loop ending brace }//while loop ending brace } }
Do you have any suggestions on how I should make the code work the way I want it?
This is a classic homework task - so I added the tag.
But you provided code -> +5 for that!
You are using a while loop - that one runs until the list is done.
So just switch that one to a for loop to get the desired 12 lines running.
Or add a counter and count the number of lines you've run through.
there is another for loop in there:
for (int counter=0; counter<=42; counter++){ // fancy code }
Why is that one counting to 42?
这篇关于这种语法可行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!