S
S
Slasherr2017-04-16 23:47:28
Java
Slasherr, 2017-04-16 23:47:28

How to add a loop?

Hello.
For the university I do D / Z. It is necessary to write a program for reading numbers for simplicity.
Here's what happened:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;


class Codechef
{
  public static void main (String[] args) throws java.lang.Exception
  {
            Scanner scan = new Scanner(System.in); 
            System.out.println("Enter a number: "); 
            int input = scan.nextInt(); 
            boolean prime = true; 
            for (int i = 2; i<input; i++){ 
            if(input % i==0){ 
            prime = false; 
            } 
            } 
            if(prime==false){ 
            System.out.println(input + " is not prime"); 
            } 
            else if(prime==true){ 
            System.out.println(input + " is prime");
  }
}
}

When I enter 1, it says to me that it is simple. But as far as I know, 1 is neither prime nor compound.
1. How can I add an "exception" for 1, so that when you enter it, it says that it is neither simple nor compound?
2. Do I need to create a separate cycle for it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jan, 2017-04-17
@Stairdeck

At you from infinity of variants one case is not processed. Why loop over one value when you know it's one? Make an exception, of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question