简体   繁体   中英

Scope of variables and Passing values in java

I'm trying to get better at using OOP with Java. My goal in this exercise is to take user input and check if it is prime using an boolean method. I may have approached this wrong but any advice on what I've created would be most beneficial because I feel like I'm on the right track.

In this program I have two main problems

First, I would like to simply get the isPrime method to be error free. With how I generally approach problems, I have a hard time setting up boolean methods.

The error comes in the last statement because prime variable has not been initialized. But I would like to get the verification whether the variable prime is true or false from the main if-else block though! Is there a way to get that value out of if-else block's scope?

public boolean isPrime(int number){
    boolean prime;
    if(number == 2){
        System.out.println( number + " is prime");
        prime = true;
    } else{
        for(int i = 2; i < number; i++){
            if( number % i == 0){
                System.out.println(number + " is composite");
                prime = false;
                break;
            } else
                System.out.println( number + " is prime");
            prime = true;
       }
    }

    return prime;
}

My second huge problem how do I call the isPrime method to print my desired results using user input?

I've been working on this for hours and I can fix one thing and then another error pops up somewhere.

Here's the entire program. The main method is bare because I erased my previous attempts to use the isPrime method. Here it is:

import java.util.*;

public class PrimeChecker{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out
           .println("Enter a number to see if it is Prime or Composite: ");
        int check = sc.nextInt();
        sc.close();
    }

    public boolean isPrime(int number){
        boolean prime;
        if(number == 2){
            System.out.println( number + " is prime");
            prime = true;
        } else{
            for(int i = 2; i < number; i++){
                if( number % i == 0){
                    System.out.println(number + " is composite");
                    prime = false;
                    break;
                } else
                    System.out.println( number + " is prime");
                prime = true;
           }
        }

        return prime;
    }
}

What happens if number is lesser than 2? The variable prime is not defined because the code doesn't enter into the for loop. Try declaring prime as true by default and setting it to false when you reach that indeed it is not a prime number.

public boolean isPrime(int number){
     boolean prime = true;
           if(number == 2){
               System.out.println( number + " is prime");
               prime = true;
           }    else{
                       for(int i = 2; i < number; i++){
                            if( number % i == 0){
                                System.out.println(number + " is composite");
                                prime = false;
                                break;
                            }
                       }

                }
    return prime;
}

For your second question try:

PrimeChecker checker = new PrimeChecker();
boolean isPrime = checker.isPrime(check);

When you print number + " is prime" in the else block, you don't yet know that the number is prime. You only know that you haven't yet proven that it's composite. Also, you're probably unable to call isPrime because you've made it an instance method. It should be static :

public static boolean isPrime(int number){
    if (number != 2) {
        for (int i = 2; i < number; i++) {
            if (number % i == 0) {
                System.out.println(number + " is composite");
                return false;
            }
        }
    }
    System.out.println(number + " is prime");
    return true;
}

Then inside your main() :

if (isPrime(check)) {
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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