简体   繁体   English

Java和For循环

[英]Java and For Loops

I am trying to write a short program that finds the multiples between two numbers not including those numbers. 我正在尝试编写一个简短的程序,以查找两个数字(不包括这些数字)之间的倍数。

import java.util.*;
  public class MinilabLoops
   {
    public static void main(String []args)
    {
      Scanner kb = new Scanner(System.in);

      System.out.print("Please enter 2 integers (separated by spaces): ");
      int numFirst = kb.nextInt();
      int numSecond = kb.nextInt();

      System.out.println("\nThis program will generate numbers BETWEEN\n" + numFirst + " and " + numSecond + " that are divisible by a certain number.");

      System.out.print("\nPlease enter the integer your output should be divisible by: ");
    int numDivisible = kb.nextInt();

      while (numDivisible == 0)
      {
          System.out.println("You cannot divide by 0, Please try again. ");
        numDivisible  =  kb.nextInt();
      }

      if (numFirst  <= numSecond)
          {
              for (int x = numFirst  ; x != numSecond ; x++)
                  if (x % numDivisible == 0)
                    System.out.println(x);
          }
      else if (numFirst >= numSecond)
          {
              for (int y = numFirst ; y != numSecond ; y--)
                  if (y % numDivisible == 0)
                      System.out.println(y);
         }
       }
    }

In the last "else if" When ever I enter 21 and -7 and have the divisiable be 7 it would always print out 21 14 7 0. What I want it only 14 7 and 0. 在最后一个“ else if”中,无论何时我输入21和-7并将可除数设置为7,它总是会打印出21 14 70。我只希望它输出14 7和0。

Please help thanks. 请帮忙谢谢。

Begin your for loop at numFirst-1 . numFirst-1开始for循环。

for (int y = numFirst - 1 ; y != numSecond ; y--)

Second option would be to add a test to your if, but not a fan: 第二种选择是向您的if添加测试,但不添加粉丝:

if (y % numDivisible == 0 && y != numFirst)

After this code: 此代码后:

while (numDivisible == 0)
{
    System.out.println("You cannot divide by 0, Please try again. ");
    numDivisible  =  kb.nextInt();
}

you should rewrite your loops. 您应该重写循环。

I suggest you swap numFirst and numSecond instead of writing 2 loops (unless you do this to reverse the order of printing): 我建议您交换numFirstnumSecond而不是编写2个循环(除非您这样做是为了反转打印顺序):

if (numSecond < numFirst)
{
    int temp = numFirst; numFirst = numSecond; numSecond = temp;
}

A simple change is to add a +1 to x at the start of the loop: 一个简单的更改是在循环开始时向x添加+1:

for (int x = numFirst+1  ; x != numSecond ; x++)
                  if (x % numDivisible == 0)
                    System.out.println(x);

For a more efficient solution, try: 要获得更有效的解决方案,请尝试:

// find smallest value > x which is divisible by numDivisible
int x = (numFirst/numDivisible+1)*numDivisible;

Then jump by numDivisible in your loop: 然后在循环中按numDivisible跳转:

for (int x = (numFirst/numDivisible+1)*numDivisible; x < numSecond ; x+=numDivisible)
                    System.out.println(x);

This resolves the problem: 这样可以解决问题:

import java.util.*;
public class numbers {
    public static void main(String [] args) {
        Scanner kb = new Scanner(System.in);
        System.out.print("Please enter 2 integers (separated by spaces): ");
        int n1 = kb.nextInt();
        int n2 = kb.nextInt();

        int a = n1 >= n2 ? n2 : n1;
        int b = n1 >= n2 ? n1 : n2;

        System.out.println("\nThis program will generate numbers BETWEEN\n" + a + " and " + b + " that are divisible by a certain number.");

        System.out.print("\nPlease enter the integer your output should be divisible by: ");
        int divisor = kb.nextInt();
        while (divisor == 0) {
           System.out.println("You cannot divide by 0, Please try again. ");
           divisor =  kb.nextInt();
        }

        for (int n = a + divisor; n < b; n += divisor) {
            if (n % divisor == 0) {
                System.out.println(n);
            }
        }
    }
}

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

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