Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算用户使用Java输入的字符时出错_Java_Count_Character - Fatal编程技术网

计算用户使用Java输入的字符时出错

计算用户使用Java输入的字符时出错,java,count,character,Java,Count,Character,我应该创建一个计算特定类型字符数量的程序 由用户输入。大写字母、小写字母、数字的数量 (0到9)和除#符号以外的其他字符被计数。用户输入#退出 import java.util.Scanner; public class countchars { public static void main (String args[]) { Scanner input = new Scanner (System.in); char sym; int up = 0;

我应该创建一个计算特定类型字符数量的程序 由用户输入。大写字母、小写字母、数字的数量 (
0
9
)和除
#
符号以外的其他字符被计数。用户输入#退出

import java.util.Scanner;
public class countchars
{
    public static void main (String args[])
    {
    Scanner input = new Scanner (System.in);

    char sym;
    int up = 0;
    int low = 0;
    int digit = 0;
    int other = 0;

    System.out.print("Enter a character # to quit: ");
    sym = input.next().charAt(0);

    while(sym != '#')
    {
    System.out.print("Enter a character # to quit: ");
    sym = input.next().charAt(0);

    if (sym >= 'a' && sym <= 'z')
        {
        low++;
        }   
    } 

    System.out.printf("Number of lowercase letters: %d\n", low);
    }
}
import java.util.Scanner;
公共类countchars
{
公共静态void main(字符串参数[])
{
扫描仪输入=新扫描仪(System.in);
字符符号;
int up=0;
int低=0;
整数位数=0;
int-other=0;
System.out.print(“输入字符#退出:”);
sym=input.next().charAt(0);
而(sym!='#')
{
System.out.print(“输入字符#退出:”);
sym=input.next().charAt(0);
如果(sym>='a'&&sym您已呼叫

input.next()
第一次计数两次,因此第一个字符将被丢弃,将计数弄乱一个。

您已调用

input.next()
第一次计数两次,因此第一个字符将被丢弃,将计数弄乱一个。

如下更改

while(sym != '#')
    {

    if (sym >= 'a' && sym <= 'z')
        {
        low++;
        }

    System.out.print("Enter a character # to quit: ");
    sym = input.next().charAt(0);

    }
while(sym!=“#”)
{
如果(sym>='a'&&sym像这样变化

while(sym != '#')
    {

    if (sym >= 'a' && sym <= 'z')
        {
        low++;
        }

    System.out.print("Enter a character # to quit: ");
    sym = input.next().charAt(0);

    }
while(sym!=“#”)
{

如果(sym>='a'&&sym不使用两次输入。下一步()

用这个

sym = input.next().charAt(0);

    while(sym != '#')
    {
    System.out.print("Enter a character # to quit: ");
    //sym = input.next().charAt(0); removed this line and try

    if (sym >= 'a' && sym <= 'z')
        {
        low++;
        }


    } 
sym=input.next().charAt(0);
而(sym!='#')
{
System.out.print(“输入字符#退出:”);
//sym=input.next().charAt(0);删除此行并重试

如果(sym>='a'&&sym不使用两次输入。下一步()

用这个

sym = input.next().charAt(0);

    while(sym != '#')
    {
    System.out.print("Enter a character # to quit: ");
    //sym = input.next().charAt(0); removed this line and try

    if (sym >= 'a' && sym <= 'z')
        {
        low++;
        }


    } 
sym=input.next().charAt(0);
而(sym!='#')
{
System.out.print(“输入字符#退出:”);
//sym=input.next().charAt(0);删除此行并重试

if(sym>='a'&&sym您可以尝试在if语句中添加print语句以及low++以确认它输入语句的次数:)家庭作业问题?应标记。@EvanKnowles
家庭作业
标记已过时->您可以尝试将打印语句与low++一起添加到if语句中,以确认它输入语句的次数:)家庭作业问题?应标记。@EvanKnowles
家庭作业
标记已过时->