Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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 - Fatal编程技术网

JAVA-用扫描器读取字符

JAVA-用扫描器读取字符,java,Java,这是我正在研究的问题 我们必须要求用户输入一个字符串,然后输入一个字符(任何字符都可以)。然后计算该字符在扫描仪中出现的次数 我搞不懂如何将字符添加到扫描仪。我们还没有做阵列,所以我不想去那里,但这是我到目前为止所做的: import java.util.Scanner; public class Counter { public static void main (String args[]){ String a; char b; i

这是我正在研究的问题

我们必须要求用户输入一个字符串,然后输入一个字符(任何字符都可以)。然后计算该字符在扫描仪中出现的次数

我搞不懂如何将字符添加到扫描仪。我们还没有做阵列,所以我不想去那里,但这是我到目前为止所做的:

import java.util.Scanner;

public class Counter {

   public static void main (String args[]){

        String a;
        char b;
        int count;
        int i;


        Scanner s = new Scanner (System.in);


        System.out.println("Enter a string");

        a = s.nextLine();

        System.out.println("Enter a character");

        b = s.next().charAt(0);

        count = 0;
        for (i = 0; i <= a.length(); i++){

            if (b == s.next().charAt(b)){

                count += 1;

        System.out.println(" Number of times the character appears in the string is " + count);

                else if{

                    System.out.println("The character appears 0 times in this string");
            }


            }

        }
import java.util.Scanner;
公共课柜台{
公共静态void main(字符串参数[]){
字符串a;
字符b;
整数计数;
int i;
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入字符串”);
a=s.nextLine();
System.out.println(“输入字符”);
b=s.next().charAt(0);
计数=0;

对于(i=0;i要验证输入[String,char],请使用while循环从用户处获取字符。基本上,您将检查用户是否为字符输入输入长度为1的字符串。 以下是代码的编译和运行版本:

import java.util.Scanner;

public class Counter
{
    public static void main ( String args[] )
    {
        String a = "", b = "";
        Scanner s = new Scanner( System.in );

        System.out.println( "Enter a string: " );
        a = s.nextLine();
        while ( b.length() != 1 )
        {
            System.out.println( "Enter a single character: " );
            b = s.next();
        }

        int counter = 0;
        for ( int i = 0; i < a.length(); i++ )
        {
            if ( b.equals(a.charAt( i ) +"") )
                counter++;
        }
        System.out.println( "Number of occurrences: " + counter );
    }
}
import java.util.Scanner;
公共课柜台
{
公共静态void main(字符串参数[])
{
字符串a=“”,b=“”;
扫描仪s=新扫描仪(System.in);
System.out.println(“输入字符串:”);
a=s.nextLine();
而(b.长度()!=1)
{
System.out.println(“输入单个字符:”);
b=s.next();
}
int计数器=0;
对于(int i=0;i
首先,for循环条件应更改为:

for (i = 0; i < a.length(); i++)
在这里,与其他解决方案相比,char要比要比较的字符串便宜

第三,在for循环之后,输出取决于计数:

if (count > 0)
    System.out.println(" Number of times the character appears in the string is "
                        + count);
else // must be count == 0
    System.out.println("The character appears 0 times in this string");

我强烈建议您从可编译的代码开始,即使它可能还没有达到您想要的效果。至少通过编译代码,您可以运行它以查看它是否有效。您的代码只需要进行一些小的修改即可编译。
if (count > 0)
    System.out.println(" Number of times the character appears in the string is "
                        + count);
else // must be count == 0
    System.out.println("The character appears 0 times in this string");