4位Java计数器

时间:2015-02-18 01:13:55

标签: java

您好我正在编码,我需要一个四位数的计数器,当调用该方法时每次增加1,直到我达到9999.例如,在我调用方法之前,计数器的值将是0000.在我调用方法后,它将增加到0001,然后是0002,依此类推。

我还希望能够单独拨打每个数字。我不知道如何做到这一点,如果有人可以帮助我,那就太棒了。

我尝试过做什么:

private int[] count = new int[4];
private int counter;

private void countUp() {
    count[counter++];
    if (counter > count.length -1) {
        counter = 0;
    }
}

8 个答案:

答案 0 :(得分:0)

尝试这个简单的解决方案:

private int[] count = new int[4];
private int counter;

private void countUp() {
  counter++;
  count[0] = counter %10;
  count[1] = counter /10 % 10;
  count[2] = counter /100 % 10;
  count[3] = counter /1000 % 10;
}

答案 1 :(得分:0)

没有帮助你,但如果你想要一系列0000,0001,0002,0003,...9999,请试试这个:

    String[] counter=new String[10000];

    for (int i=0;i<=9999;i++){
        counter[i]=String.format("%04d", i);
    }

答案 2 :(得分:0)

Ngygens每个阵列不会有一位数。每个阵列位有一位数

private int[] count = new int[4];
private int counter;

private void countUp() {
  counter++;
  count[3] = counter /1000;
  count[2] = (counter /100) % 10;
  count[1] = (counter /10) % 10;
  count[0] = counter %10;
}

答案 3 :(得分:0)

如果你想要一个每次增量都不需要繁重计算的解决方案。此外,如果您想要一个不同于9999的最大值,它是完全可扩展的。

private int counter;
private int max = 9999;

private void countUp() {
    if (counter < max)
        counter++;
}

private int getDigit(int n) {
    String counterAsString = String.valueOf(counter);
    if (n < counterAsString.length())
        return Integer.parseInt(String.valueOf(counterAsString.charAt(n)));
    return 0;
}

答案 4 :(得分:0)

我会采用面向对象的方法。这将允许您访问方法以轻松增加数字,查看特定数字等。虽然米是一个整数,绑定变量将使仪表保持四位数。

public class Counter {
    private int meter = 0;
    private boolean bind = true;
    public static final int DIGIT_1 = 1000;
    public static final int DIGIT_2 = 100;
    public static final int DIGIT_3 = 10;
    public static final int DIGIT_4 = 1;

    /*
     * Look at a particular digit's value
     */
    public int peek(int digit) {
        return (meter / digit) % 10;
    }

    /*
     * By default, increments by 1. 
     */
    public void increment() {
        this.increment(Counter.DIGIT_4);
    }

    /*
     * Increments one of the digits
     */
    public void increment(int digit) {
        if(digit != Counter.DIGIT_1 
            && digit != Counter.DIGIT_2 
            && digit != Counter.DIGIT_3 
            && digit != Counter.DIGIT_4) {
            return;
        }
        this.meter += digit;
        if(bind) {
            this.meter = Math.abs(this.meter % 10000);
        }
    }

    /*
     * By default, adds the number to the right-most digit. 
     */
    public void add(int number) {
        this.add(number, Counter.DIGIT_4);
    }

    public void add(int number, int digit) {
        if(digit != Counter.DIGIT_1 
            && digit != Counter.DIGIT_2 
            && digit != Counter.DIGIT_3 
            && digit != Counter.DIGIT_4) {
            return;
        }
        this.meter += (digit * number);
        if(bind) {
            this.meter = Math.abs(this.meter % 10000);
        }
    }

    /*
     * Prints out the counter as four numbers
     */
    @Override
    public String toString() {
        return String.format("%04d", this.meter);
    }

    /*
     * An example
     */
    public static void main(String[] arg) {
        Counter c = new Counter();
        c.add(-15000);
        c.increment(Counter.DIGIT_2);
        System.out.println(c + ": " + c.peek(Counter.DIGIT_4));
    }
}

答案 5 :(得分:0)

您不需要数组:

private String count = "0000";
private int counter;

private void countUp() {
    if (9999 < counter) {
        return;
    }
    counter ++;
    count = String.format("%04d", counter);
}

答案 6 :(得分:0)

试试这个:

Integer numwithoutzero;
char[] things = new char[4];
int zerocount = 4;
private void countUp() {
char[]c = String.valueOf(numwithoutzero).toCharArray();
for(int i = 0;i<=zerocount;i++)things[i]='0';
for(int i = 0;i<=String.valueOf(numwithoutzero).length();i++){
for(int b = c.length;b>=0;b--){
things[b] = c[i];
}
}
numwithoutzero++;
zerocount = 4 - c.length
}

答案 7 :(得分:0)

import java.util.Scanner;

public class count0001to0002{
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number: ");
        int num = sc.nextInt();

        int[] count = new int[4];

        for(int x=0; x<=num; x++){
            int counter = x;
            count[3] = counter %10;
            count[2] = counter /10 % 10;
            count[1] = counter /100 % 10;
            count[0] = counter /1000 % 10;
            for(int y=0; y<4; y++){
                System.out.print(count[y]);
            }
            System.out.println();
        }
        for(int x=0; x<4; x++){
            System.out.print(count[x]);
        }
    }
}