0

I'm a beginner java developer. I want to build a program that gets a number from the user, then say it's prime or not.

Java code:

import java.util.Scanner;
import static java.lang.System.out;

public class prime
{
    public static boolean prime(int n)
    {
        for(int i = 2; i <n ; i++)
        {
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
    public static void main(String[]args)
    {
        Scanner input = new Scanner(System.in);
        out.println("enter a number: ");
        int x = input.nextInt();
        if(prime(x)){
            out.println(x + "is a prime number");
        }else{
            out.println(x + "isn't a prime number");
        }
    }
}

However, I want to declare a bool variable, then ask the user if they want to continue, the user then says yes or no. I have already written this code in C#:

C# code

class Program
{
    static bool prime(int n)
    {
        for(int i = 2; i < n ; i++)
        {
            if(n % i == 0)
       ‌{
                return false;
            }
        }
        return true;
    }
    static void main(String[]args)
    {
    Bool permit = true;
    While(permit)
    {
    Console.WriteLine(“enter a number”)
        int x = int.Parse(Console.ReadLine());
        if(prime(x))
    {
            Console.WriteLine(x + "is a prime number");
        }
    else
    {
            Console.WriteLine(x + " isn't a prime number");
        }
    Console.WriteLine(“do you want to continue”);
    Permit = Console.ReadKey.Key() == ConsoleKey.Y?true:false;
    }   
    }
}

How can I build it in Java?

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158

3 Answers3

2

You can't directly map your C# keyboard key press detection to Java.

AFAIK, checking which key is pressed can only be done via key listeners in Java AWT/Swing GUI programs [How to Write a Key Listener]. Your program, however, is a console program and Java doesn't have any mechanisms to detect which key was pressed in a console application. See this question for more info.

Now what you could do is read the String that a certain key press produced. Something like this:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    boolean permit = true;
    while (permit) {
        // your existing code
        out.println("do you want to continue?");
        permit = input.next("y|Y").equalsIgnoreCase("y");
    }
}
Janez Kuhar
  • 2,202
  • 2
  • 16
  • 32
0

I also corrected prime method.

import java.util.Scanner;
import static java.lang.Math.abs;
import static java.lang.System.out;

public class Prime {
   public static boolean prime(int n) {
    for (int i = 2; i < n/2; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

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

    while (true) {
        try {
            out.println("enter a number: ");
            int x = Integer.parseInt(input.nextLine());
            if (prime(x)) {
                out.println(x + " is a prime number");
            } else {
                out.println(x + " isn't a prime number");
            }
        } catch (NumberFormatException e) {
            System.out.println("NumberFormatException");
        }
        out.println("do you want to continue");
        if (!input.nextLine().equals("Y")) break;
    }
  }
}
Sergey Afinogenov
  • 1,444
  • 2
  • 7
  • Interesting "correction" because there are various views about whether negative numbers can be prime or not (all views are valid) - see https://primes.utm.edu/notes/faq/negative_primes.html – k314159 Feb 09 '21 at 13:59
  • Yes, now I'm not sure about abs. But I suppose it is enough to iterate till i<=n/2 – Sergey Afinogenov Feb 09 '21 at 14:14
  • 1
    You can use `i < n/2` instead of `i <= n/2`. The only difference it could make to the result is if `n % i == 0` when `i == n/2`. But that case would have already been caught in the iteration when `i = 2`. – k314159 Feb 09 '21 at 14:39
  • Yes, I think you right. In both cases we check if number is even. – Sergey Afinogenov Feb 09 '21 at 14:45
-1

Just check if the key pressed is the y (yes) and if yes it continues in the cycle.

static void main(String[] args)
{
    ConsoleKey response;
    
    do
    {
        Console.WriteLine("enter a number");
        int x = int.Parse(Console.ReadLine());
        
        if (prime(x)) {
            Console.WriteLine(x + " is a prime number");
        } else {
            Console.WriteLine(x + " isn't a prime number");
        }

        Console.Write("do you want to continue? [y/n] ");
        response = Console.ReadKey(false).Key;  
    }
    
    while (response == ConsoleKey.Y);
}
Michael
  • 2,758
  • 6
  • 28
  • 55