java程序运行错误?统计输入一行字符串中大小写字母个数和数字个数 一起其他字符个数

package mop;
import java.util.Scanner;
public class e4 {
public static void main(String[]args)
{
System.out.println("enter :");
Scanner scan=new Scanner(System.in);
String str=new String();
str=scan.nextLine();
System.out.println(str);
scan.close();
int d=0;//数字
int s=0;//小写字母
int x=0;//大写字母
int o=0;//其他字符
while(str!=null)
{
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>=0 && str.charAt(i)<=9)
d++;
if(str.charAt(i)>='a' && str.charAt(i)<='z')
s++;
if(str.charAt(i)>='A' && str.charAt(i)<='Z')
x++;
else
o++;
}
}
System.out.println(" big character:"+x+"small character:"+s+"data :"+d+"other"+o);

}
}

有三个问题:
一是while(str != null){}是个死循环;

二是你判断数字字符有问题:

if(str.charAt(i)>=0 && str.charAt(i)<=9)
    d++;

数字字符'1'的ASCII码值是49,而根据你的判断‘1’就不是数字字符;

三是你的控制逻辑有问题:

按照你的控制逻辑,只要是不满足第三个if条件的字符都是其他字符。

下面是修改过的例子:

package test;
import java.util.Scanner;
public class TestAgain {
public static void main(String[]args)
{
System.out.println("enter :");
Scanner scan=new Scanner(System.in);
String str=new String();
str=scan.nextLine();
scan.close();
System.out.println(str);
int d=0;//数字
int s=0;//小写字母
int x=0;//大写字母
int o=0;//其他字符
for(int i=0;i<str.length();i++)
{
if(String.format("%c", str.charAt(i)).matches("[0-9]"))
d++;
else if(str.charAt(i)>='a'  && str.charAt(i)<='z')
s++;
else if(str.charAt(i)>='A' && str.charAt(i)<='Z')
x++;
else
o++;
}
System.out.println(" big character:"+x+"\tsmall character:"+s+"\tdata:"+d+"\tother"+o);
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-26
死循环,哥哥
package file;
import java.util.Scanner;
public class ReadData {
public static void main(String[]args)
{
System.out.println("enter :");
Scanner scan=new Scanner(System.in);
String str=new String();
str=scan.next();
System.out.println(str);
scan.close();
int d=0;//数字
int s=0;//小写字母
int x=0;//大写字母
int o=0;//其他字符

for(int i=0;i<str.length();i++)
{
if(str.charAt(i)>='0' && str.charAt(i)<='9'){
d++;
}
else if(str.charAt(i)>='a' && str.charAt(i)<='z'){
s++;
}
else if(str.charAt(i)>='A' && str.charAt(i)<='Z'){
x++;
}
else{
o++;
}
}
System.out.println(" big character:"+x+"small character:"+s+"data :"+d+"other"+o);

}
}本回答被网友采纳
第2个回答  2013-11-26
for(int i=0;i<str.length();i++)循环用于统计字符串中各种字符数量,while(str!=null)循环中,若str!=null条件为真,则会因为循环中无跳出条件而变成死循环,故会不断执行而出错
第3个回答  2013-11-26

在while处加一个标记"exit"即可

package com.baidu.zhidao;

import java.util.Scanner;

/**
 * 统计输入一行字符串中大小写字母个数和数字个数 一起其他字符个数
 * 
 * @author
 * 
 */
public class TestScanner {
public static void main(String[] args) {
System.out.println("enter :");
Scanner scan = new Scanner(System.in);
String str = new String();
str = scan.nextLine();
System.out.println(str);
scan.close();
int d = 0;// 数字
int s = 0;// 小写字母
int x = 0;// 大写字母
int o = 0;// 其他字符
while (str != "exit") {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 0 && str.charAt(i) <= 9)
d++;
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
s++;
if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
x++;
else
o++;
}
str = "exit";
}
System.out.println("big character:" + x + "\nsmall character:" + s
+ "\ndata :" + d + "\nother" + o);

}
}

第4个回答  2013-11-26
运行错误吗?我怎么看着是编译错误呢?
相似回答