1

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

我编写的程序要求用户输入一个数字,而不是程序应该输出从 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;
}
4

5 回答 5

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();
    }
}

如果答案正确,不要忘记接受答案.. ^^

于 2013-06-04T12:18:11.857 回答
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);
    }
}
于 2013-06-04T12:18:44.707 回答
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);    
  }
}
于 2013-06-04T12:20:18.187 回答
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();
于 2013-06-04T12:13:45.980 回答
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();
            }
        }

但是再次感谢大家提出我的想法。

于 2013-06-04T12:42:52.610 回答