如何在java中查看字符串的数字是否比其他符号多
所以对于一个作业,我必须做一个程序,要求输入一个字符串,然后检测回文 问题是,数字也可以输入。当字符串输入的一半以上是数字时,它需要将字符串视为数字字符串,而忽略其他符号 所以我的想法是将输入字符串放入一个数组中,然后查找数字(ASCII#介于48和57之间)并对其进行计数。然后比较数字的数量和字母的数量,看看哪一个有更多 然而,我似乎无法编程它在一个字符串中计算数字。有人能帮我吗,我已经有了这个:如何在java中查看字符串的数字是否比其他符号多,java,numbers,Java,Numbers,所以对于一个作业,我必须做一个程序,要求输入一个字符串,然后检测回文 问题是,数字也可以输入。当字符串输入的一半以上是数字时,它需要将字符串视为数字字符串,而忽略其他符号 所以我的想法是将输入字符串放入一个数组中,然后查找数字(ASCII#介于48和57之间)并对其进行计数。然后比较数字的数量和字母的数量,看看哪一个有更多 然而,我似乎无法编程它在一个字符串中计算数字。有人能帮我吗,我已经有了这个: public class opgave41 { public static void ma
public class opgave41 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("input a string:");
String reeks = sc.nextLine();
char[] array1 = reeks.toCharArray();
int numbers;
int other;
for(int i=0;i<array1.length;i++){
if (int array1[i] < 57 || int array1[i] > 48)
numbers++;
else
other++;
}
System.out.prinln(numbers);
System.out.prinln(other);
}
公共类opgave41{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入字符串:”);
字符串reeks=sc.nextLine();
char[]array1=reeks.toCharArray();
整数;
int其他;
对于(int i=0;i 48)
数字++;
其他的
其他++;
}
系统输出prinln(数字);
系统输出prinln(其他);
}
}
如果我编译它,我会得到:
opgave41.java:38: '.class' expected
if (int array1[i] < 57 || int array1[i] > 48)
^
opgave41.java:38:'应为'.class'
if(int-array1[i]<57 | | int-array1[i]>48)
^
opgave41.java:39:“)”应为
数字++^ 2个错误
如何才能使其正常工作?您正在检查
array1.length
这是字符数组的长度。我不认为那是你想要的。您希望array1[i]
获取当前字符。另外,我认为你有这样的倒退:
if (int array1.length < 57 || int array1.length > 48)
if(int-array1.length<57 | | int-array1.length>48)
假设您将其修复为使用
array1[i]
,它仍将检查当前字符是否为而不是数字。但是,然后继续递增数字
计数器。此外,您还可以考虑使用<代码>字符。ISDigiE()/代码>方法。 < P>逻辑是OK *,您只需要使用<代码> char < /Case>数组的元素而不是数组长度。那就换一个吧
if (int array1.length < 57 || int array1.length > 48)
if(int-array1.length<57 | | int-array1.length>48)
与
if(数组1[i]=“0”)
(请注意&&
,因为您希望这两个条件都为真。如果使用|
,则表示小于9或大于0,这对于任何字符都是真的。)
*除了一些语法错误在修复了明显的语法错误后,我根据您的代码得到了以下代码:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("input a string:");
String reeks = sc.nextLine();
int numbers = 0;
int other = 0;
for (char c : reeks.toCharArray()) {
if ('0' <= c && c <= '9')
numbers++;
else
other++;
}
System.out.println(numbers);
System.out.println(other);
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入字符串:”);
字符串reeks=sc.nextLine();
整数=0;
int-other=0;
for(char c:reeks.toCharArray()){
如果('0'不需要循环、检查等,那么使用正则表达式来处理数字就容易多了
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("input a string:");
String reeks = sc.nextLine();
String symbols = reeks.replaceAll("[0-9]", "");
System.out.println("others - " + symbols.length());
System.out.println("numbers - " + (reeks.length() - symbols.length()));
}
只是一个旁侧节点:你的代码中有很多拼写错误。你试过使用Eclipse或其他集成开发环境吗?这些会告诉你许多错误在哪里,并突出显示它们。是的,我现在意识到了这一点,只是从学习java开始,我使用textmate。我的老师不喜欢Eclipse,我试过,但它太多了y、 你能给我推荐一个好的编辑器吗?除了写文本,没有其他很多功能,比如颜色/缩进等。我不太喜欢textmate,因为它不会自动添加{或(等)。另外,我有MAC OSX
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("input a string:");
String reeks = sc.nextLine();
String symbols = reeks.replaceAll("[0-9]", "");
System.out.println("others - " + symbols.length());
System.out.println("numbers - " + (reeks.length() - symbols.length()));
}