在这种情况下,我不能使用for循环或do while循环。我在一个介绍编程课,似乎无法为我工作,我需要一些帮助。我们要做的是获取一个文本文件make java read it并找出有多少左括号,右括号,逗号,句号,感叹号,问号,星号以及文本文件中有多少个元音。我面临的问题是,我似乎无法更新计数器更正,我也无法将input.hasNext()作为变量。任何事都有帮助,提前谢谢。
代码在这里:
import java.util.Scanner;
import java.io.*;
public class PP5_15
{
public static void main(String[]args) throws IOException
{
Scanner input = new Scanner(new File("Chp5.txt"));
input.useDelimiter("");
int R_ParCount = 0;
int L_ParCount = 0;
int ComCount = 0;
int PerCount = 0;
int ExCount = 0;
int QuCount = 0;
int AstCount = 0;
int VowCount = 0;
int NumCount = 0;
while(input.hasNext() == true)
{
if(input.next().contains("("))
R_ParCount++;
else if (input.next().contains(")"))
L_ParCount++;
}
while(input.hasNext())
{
if(input.next().contains("'"))
ComCount++;
else if(input.next().contains("%"))
PerCount++;
}
System.out.println("Amount of Right Parenthese: "+R_ParCount);
System.out.println("Amount of Left Parenthese: "+L_ParCount);
System.out.println("Amount of Commas: "+ComCount);
System.out.println("Amount of Percent Signs: "+PerCount);
System.out.println("Amount of Exclamation Points: "+ExCount);
System.out.println("Amount of Question Marks: "+QuCount);
System.out.println("Amount of Astric Count: "+AstCount);
System.out.println("Amount of Vowels: "+VowCount);
System.out.println("Amount of Numeric Places are: "+NumCount);
}
}
答案 0 :(得分:2)
您只需存储if($result->num_rows >0){
$json=[];
while(($row = $result->fetch_assoc())) {
$json[] = $row;
}
echo json_encode($json);
} else {
echo "0 results";
}
返回的值,然后进行比较:
next()
答案 1 :(得分:0)
import java.util.Scanner;
import java.io.*;
public class CountChars
{
public static void main(String[]args) throws IOException
{
// fine so far
Scanner input = new Scanner (new File("meanVarianceRange.sh"));
input.useDelimiter("");
// counting everything is more easy, than counting 10 different things
int[] charcount = new int [256];
// except for the vowels, which is aeiou=5 + 5 (uppercase)
int vowels = 0;
// not to mention 10 ciphers, if I understood correctly
int numerics = 0;
while (input.hasNext ())
{
char c = input.next ().charAt (0);
// we use the char as index into our array and just increment
++charcount [c];
// but for the vowels, we count them as group, too
if ("aeiouAEIOU".contains ("" + c))
++vowels;
// in contrast, digits are ordered in the ascii table,
// so we can express them as >= '0' and <= '9'
if (c >= '0' && c <= '9')
++numerics;
}
// see, how the char itself makes the code self commenting:
System.out.println ("Amount of Right Parenthese: " + charcount[')']);
System.out.println ("Amount of Left Parenthese: " + charcount['(']);
System.out.println ("Amount of Commas: " + charcount[',']);
System.out.println ("Amount of Percent Signs: " + charcount['%']);
System.out.println ("Amount of Exclamation Points: " + charcount['!']);
System.out.println ("Amount of Question Marks: " + charcount['?']);
System.out.println ("Amount of Astric Count: " + charcount['*']);
System.out.println ("Amount of Vowels: " + vowels);
// (charcount['a'] + charcount['a'] + charcount['e'] + charcount['i'] + charcount['o'] + charcount['u']));
System.out.println ("Amount of Numeric Places are: " + numerics);
}
}
由于String中的'contains'方法需要一个String,我们必须使用从char到String(长度为1)的这种特殊转换来查找我们的char:.contains ("" + c)
。好吧 - 有一些替代品,比如indexOf和测试&gt; = 0。
字符可以完美地转换为整数(但不是相反的方式)。这就是为什么我们可以使用char本身作为我们的int数组的索引,它包含charcount ['x']的值,我们在该索引处执行增量数。
请注意,我更改了输入文件名,以匹配驱动器上的内容。
测试:
Amount of Right Parenthese: 11
Amount of Left Parenthese: 11
Amount of Commas: 0
Amount of Percent Signs: 1
Amount of Exclamation Points: 1
Amount of Question Marks: 1
Amount of Astric Count: 7
Amount of Vowels: 43
Amount of Numeric Places are: 19