0

I need help making a loop that looks at each value from 1 to number-1. Also how to test each value to see if it is a divisor of number, and if it is, adding it to the sum.

This is what I have so far:

public static void main(String[] args) {
  Scanner input = new Scanner (System.in);

  System.out.print("Please enter a positive integer: ");
  int n = input.nextInt();

  while (n < 0) {
    System.out.println(n + " is not positive.");
    System.out.print("Please enter a positive integer: ");
    n = input.nextInt();
  }
}
ThisaruG
  • 2,382
  • 5
  • 34
  • 50
Jordon
  • 91
  • 8
  • If `n` is a positive number then your loop will run forever. You need a running index to start from 1 and a loop that runs while it's smaller than `n`. Did you learn about loops? You must have seen examples. – user1803551 Feb 08 '16 at 06:27

2 Answers2

0

You can use this as a starting block for your application:

    package Testers;

import java.io.Console;

public class Application {

    public static void main(String[] args)
    {
        Console console = System.console();
        if (console == null) 
        {
            System.err.println("No console.");
            System.exit(1);
        }

        boolean keepRunning = true;
        while (keepRunning)
        {       
            String name =  console.readLine("Type your positive integer");
            try{
            int integer = Integer.parseInt(name);
            if(integer < 0){
                System.out.println("You must specify a positive integer!");
            }
            for(int i = 1; i<integer; i++){ 
                // our variable "i" is smaller than "integer". This will parse all the numbers between one and "integer" -1.
                if(i % 2 == 0){
                    //"i" IS divisible by 2. Of course, you can change this value to what you want to change it to.
                    //Here you can add it to a sum
               }else{
                //"i" is not divisible by 2. Of course, you can change this value to what you want to change it to.
                }
            }
            }catch(NumberFormatException e){
                System.out.println("You must specify a positive integer!");
            }
        }
    }   

}
JD9999
  • 272
  • 1
  • 8
  • 16
  • (1) Use a `Scanner` instead of `Console` as it might not be available. (2) No need for `int one = 1` when it can be assigned directly to `i`. (3) Format your code properly. – user1803551 Feb 08 '16 at 10:16
  • 1
    Edited indenting and removed the "one" variable. I have never had a problem using `Console` instead of `Scanner`, I am reluctant to change that. Thank you for your help! – JD9999 Feb 08 '16 at 20:47
0

If you want to do something for a known number of times, it is mostly a good idea to use a for loop. If you want to do something for number 1 to n-1, the loop could look like

for(int i = 1; i < n; i++) { // do stuff }

Note that it starts counting from 1 and stops as soon as i is greater or equal than n.

In order to know whether a number, say n, is divisible by some number, say k, the modulo-operator % could be used. If n % k == 0 this means that n is divisible by k. With an if-statement this can be tested and when you have some sum variable you can add whatever you want to that variable to sum things up.

Hope that helps

Mr Tsjolder
  • 2,648
  • 1
  • 22
  • 41