简体   繁体   English

设定数字重复多少次的程序

[英]Program about how many times the number has been repeated

For example i want to find how many times 1 repeated in number 123900148 It must be write 2 times but i get wrong values for everytime 例如,我想找到多少次重复1,重复数123900148,它必须被写入2次,但是每次都得到错误的值

Scanner input = new Scanner(System.in);
@author Başar Ballıöz

int counter = 0;
int repeat;
int tmp;


System.out.print("Enter A Number: ");
tmp = input.nextInt();
String number = Integer.toString(tmp);


System.out.print("Enter A Number You Want To Find: ");
repeat = input.nextInt();


for (int i = 0; i < number.length() - 1 ; i++) {    

    if (number.substring(i , i+1).equals(repeat))

    counter++;
}

System.out.println(repeat + " number " + counter + " repeated.");

i would like to see my output like: 我想看到我的输出像:

number : 134211
number i want to find how many times repeated: 1
your number has repeated 3 times

You are comparing a String (returned by number.substring(i , i+1 ) to an Integer , so of course it will always return false . 您正在将一个String (由number.substring(i , i+1 )与一个Integer ,因此,它将始终返回false

Either compare two int s or two String s. 比较两个int或两个String Since you are essentially comparing two digits, comparing int s would be more efficient. 由于本质上是比较两个数字,因此比较int会更有效。

for (int i = 0; i < number.length(); i++) {    
    if (Character.getNumericValue(number.charAt(i)) == repeat) {
        counter++;
    }
}
  Scanner input = new Scanner(System.in);

    int counter = 0;
    int repeat;
    int tmp;

    System.out.print("Enter A Number: ");
    tmp = input.nextInt();
    String number = Integer.toString(tmp);

    System.out.print("Enter A Number You Want To Find: ");
    repeat = input.nextInt();

    while (tmp > 0) {

        if (tmp % 10 == repeat) {
            counter++;               
        }
        tmp = tmp/10;
    }

    System.out.println(number + " number " + counter + " repeated.");

You're comparing a String against an Integer via equals hence you're not getting the expected result. 您正在通过equalsStringInteger进行比较,因此无法获得预期的结果。 instead convert the integer to a string prior to comparison: 而是在比较之前将整数转换为字符串:

 if (number.substring(i , i+1).equals(String.valueOf(repeat)))

Further, you could cache the result of String.valueOf(repeat) into a variable before the for loop to prevent a string object construction in each iteration of the loop. 此外,您可以将String.valueOf(repeat)的结果缓存到for循环之前的变量中for以防止在每次循环迭代中构造字符串对象。

Try this. 尝试这个。 I added some helpful output so you can see how it's indexed. 我添加了一些有用的输出,以便您可以看到如何对其进行索引。

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

public class CountChars {

    public static void main(String [] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter A String: ");
        Map<String, Integer> map = indexString(input.nextLine());

        while (true) {
            System.out.print("Enter A Character You Want To Count (ENTER to exit): ");
            String repeat = input.nextLine();
            if (repeat == null || repeat.isEmpty()) {
                break;
            }
            System.out.println(String.format("'%s' was repeated %d time(s).", repeat, (map.containsKey(repeat)) ? map.get(repeat):Integer.valueOf(0)));
        }
    }

    private static Map<String, Integer> indexString(String s) {
        Map<String, Integer> map = new HashMap<>();
        System.out.println(String.format("'%s' has %d characters.  Indexing now.", s, s.length()));
        for (int i = 0; i < s.length() ; i++) {
            String c = String.valueOf(s.charAt(i));
            if (!map.containsKey(c)) {
                map.put(c, 0);
                System.out.println(String.format("Indexing %s", c));
            }
            System.out.print(String.format("Incrementing '%s' from %d ", c, map.get(c)));
            map.put(c, map.get(c) + 1);
            System.out.println(String.format("to %d.", map.get(c)));
        }
        return map;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM