如何在java中每行输出几个数字

时间:2013-06-04 12:07:39

标签: java loops for-loop while-loop

嗨,我是编程新手,所以请不要对我的愚蠢问题大笑。

我写了一个程序,要求用户输入一个数字而不是程序应输出从0到输入数字的所有数字(如果是正数或负数则无关紧要)。 我尝试了3个不同的版本

这是我的代码:

int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
int num = PutiL.validNum(min, max, "number");
//this my utility methode which check if number is in range and if it is not a double or letter
int i = 0, z;
int y = 0;

//3rd version
while (i <= num) {
    for (z = 0; z < 4; z++) {
        System.out.print(i + " ");
        i++;
    }
    System.out.println();
}

//2nd version
if (num > 0) {
    for (i = 0; i <= num; y++) {
        for (z = 0; z < 4; z++) {
           System.out.print(i + ",");
            i++;
        }
        System.out.println();
    }
} else {
    for (i = 0; i > num; y--) {
        for (z = 0; z < 4; z++) {
           System.out.print(i + ",");
            i--;
        }
        System.out.println();
    }
}

//1st version`enter code here`
if (num > 0) {
    for (i = 0; i <= num; i++)
    {
       System.out.print(i + ",");
    }
} else {
    for (i = 0; i >= num; i--) {
        System.out.print(i + ",");
    }
}
System.out.println();

问题是代码在用户输入的数字后不会直接停止。有人会给我一个提示有什么不对的提示,因为我没有更多的想法。

这是PutiL方法

public static int validNum(int min, int max, String words) {
    int num;

    do {
        System.out.println("Please enter " + words);
        while (!kb.hasNextInt()) {
            System.out.println("Please re-enter ");
            kb.nextLine();
        }
        num = kb.nextInt();

        if (num < min || num > max) {
            System.out.println("Not in range - re-enter\tproper range is "
                    + min + " - " + max);
        }

    } while (num < min || num > max);
    return num;
}

5 个答案:

答案 0 :(得分:1)

//2nd version
if (num > 0)
{
    for (i = 0; i <= num; y++)  <== THIS MAKE INFINITE too, OKAY.. ^^, change i to stop
    {
        for (z = 0; z < 4; z++)
        {
           System.out.print(i + ",");
            i++;
        }
        System.out.println();
    }
} else
{
    for (i = 0; i > num; y--) <== THIS MAKE INFINITE LOOPS, OKAY.. ^^, it must i to stop
    {
        for (z = 0; z < 4; z++)
        {
           System.out.print(i + ",");
            i--;
        }
        System.out.println();
    }
}

如果正确的话,不要忘记接受答案。^^

答案 1 :(得分:0)

“程序应该输出从0到输入数字的所有数字” 和 “我希望他们在一行中打印例如4,而不是跳到另一行”

if(num >=0) {

  for (z = 0; z <= num; z++)
  {
    System.out.print(z + " ");
    if(z > 0 && z%4==0)
      System.out.println();
  }

}
else {
  // similar loop for negatives
}
System.out.println();

答案 2 :(得分:0)

public static void main(String[] args) {
    int number = Integer.parseInt(args[0]);
    if (number >= 0) {
        for (int i = 0; i <= number; i++) {
            System.out.println(i);
        }
    } else {
        for (int i = 0; i >= number; i--) {
            System.out.println(i);
        }
    }
}

或者更简洁一点,并复制了println语句......

public static void main(String[] args) {
    int number = Integer.parseInt(args[0]);
    int increment = number >= 0 ? 1 : -1;
    for (int i = 0; i != number + increment; i += increment) {
        System.out.println(i);
    }
}

答案 3 :(得分:0)

这将为用户提供一个对话框,用户输入的值将计为零。

首先导入:

import javax.swing.JOptionPane.*;

然后:

int user_choice = Integer.parseInt(
showInputDialog(null, "Please enter a number."));

if(user_choice > 0){
  for(int temp = 0; temp <= user_choice; temp++){
  System.out.println(temp);    
  }
}
else{
  for(int temp = 0; temp >= user_choice; temp--){
  System.out.println(temp);    
  }
}

答案 4 :(得分:0)

谢谢大家的帮助我解决了这个问题我的自我很容易就是我用的代码,现在它的工作非常好

for(i = 0; i <= num; i++)
        {
            System.out.print(i + " ");
            y++;
            if(y % 4 == 0)
            {
                System.out.println();
            }
        }
        for(i = 0; i >= num; i--)
        {
            System.out.print(i + " ");
            y++;
            if(y % 4 == 0)
            {
                System.out.println();
            }
        }

但是再次感谢你们提出我的想法。