没有得到输出没有错误我在做什么错

时间:2018-12-15 07:46:36

标签: java

package com.db;
public class CountAlpha {
    public static void main(String[] args) {
        String str[] = {"aaaa4bbb3c"};
        int count =0;

        for(int i =0;i<str.length;i++) {

            for(int j = i+1;j<str.length;j++) {

                if(str[i] == str[j]) {
                    count++;
                    System.out.println(str[i]+" "+count);
                }
                else {
                    System.out.println(str[i]);
                }
            }
        }
    }
}

此代码有什么问题?我想计算字母出现次数

4 个答案:

答案 0 :(得分:1)

在这里,str是一个只有一个元素的数组。所以str.length1

因此i所取的唯一值是0。而且根本不会进入内循环,因为j1开始,而< str.length早已不是namespace App\Http\Controllers; use App\SendMessage; use App\Mail\SendEmail; use Illuminate\Http\Request; use Illuminate\Support\Facades\Mail; use App\Http\Controllers\Controller; use Session; class SendMessageController extends Controller { public function store(Request $request) { $this->validate($request, [ "email" => "required|email", "message" => "min:10", "subject" => "min:3" ]); $name = $request->name; $email = $request->email; $company = $request->company; $subject = $request->subject; $message = $request->message; Mail::to("audit@auditors.uz")->send(new SendEmail($subject, $message)); Session::flash("success", "Your email was sent"); return back(); } } ?>

答案 1 :(得分:0)

您试图遍历只有一个元素的字符串数组。您可以使用<?php require_once('email_checker.class.php'); class Task extends Threaded { private $value; public function __construct(int $i) { $this->value = $i; } public function run() { $file_lines = file('mail.txt'); $emailChecker = new emailChecker; // Make a new instance foreach ($file_lines as $line) { $response = $emailChecker->check($line); foreach($response as $result) { echo $result['query'].'-'.$result['success']."\n"; } } } } $file_lines = file('mail.txt'); # Create a pool of 4 threads $pool = new Pool(4); for ($i = 0; $i < 15000; ++$i) { $pool->submit(new Task($i)); } while ($pool->collect()); $pool->shutdown(); ,也可以将字符串转换为char数组并对其进行迭代:

charAt()

答案 2 :(得分:0)

正如达伍德所提到的那样,内部for循环永远不会执行,因此您看不到任何输出。如果要计算给定字符串中字母的出现次数,可以执行以下操作

// Store the input string in a variable of type String rather than storing in an array of String
String s = "aaaa4bbb3c";

// Convert the String to a character array so that you can loop over it easily
char[] carray = s.toCharArray();

// Declare an array to store count of each alphabet in the string
int countArray[] = new int[MAX_CHAR];

循环显示carray的字符和countArray中相应字母的递增计数。

您可以找到问题here

的完整实现

答案 3 :(得分:0)

Str是一个长度为1的数组,因此内部for循环不会被执行。

要计算字符的出现次数,可以创建一个长度为26的数组,并在更新count数组时迭代String。