简体   繁体   中英

Why does my object return 2 times, and my for loop does not get read?

import java.util.Scanner;

public class primtal {
    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    //ask for input
    System.out.println("Enter an integer: ");
    int num = input.nextInt();
    
    //call the object prime to start the sequence, and see if input is prime
    prime(num);
    
    //see if the returned value is None(not prime) or not None(prime)
    if (prime(num) == 1){
        System.out.println("It is a prime");
    }
    
    else{
        System.out.println("It is not a prime");
    }
    
    
}

public static int prime(int num){
    System.out.println("hello");
    //make an list from 0 up to num
    int[] numList = new int[num];
    for (int z = 0; z == num; z++){
        numList[z] = z;
    }
    
    //loop through all numbers up to num, and calculate which 2 multiply together
    for (int i = 1; i == num; i++){
        for (int n = 1; n == num-1; n++){
            //from here
            if (i*numList[n] == num){
                System.out.println("hit this");
                return 1;
            }else {
                System.out.println("hit this 2");
            }
            //to here; does not get read
        }
        
        System.out.println("hit this 3"); //this also deos not get read
    }
    System.out.println("hit this 4");
    return 0;
}
}

I made a similar program in python with the exact same for loop sequence, and it works fine, however, I am relatively new to Java so "translating" my code from python to java was harder than expected...

The purpose of this code is to calculate if the input is prime or not. I made an object for the sequence. If the sequence returns 1, then the number is prime, otherwise, it's not.

For some reason that I do not understand is that my object gets returned twice. All of my "hit this" and "hello" in the object get printed twice. As if I have called it twice in my main.

On the first glance, your for loop seems to be having different condition than you are mentioning in the comment of the code. The loops don't run because the condition is z==num . Which would be false if you provide any inputs other than 0. If you are trying to run a loop from 0 to num, type z<=num instead. Also, you are calling the prime function twice and not storing the call in an integer type in the first call. prime(num);

prime function is calling twice. and thats why returning two times. you need to store the value of prime method and then compare it in the if block.

int prime = prime(num);

//see if the returned value is None(not prime) or not None(prime)
if (prime == 1){
    System.out.println("It is a prime");
}

You are calling the prime(num) twice. Once directly and once in the if condition. You can remove the direct call and it will work as expected. My suggestion would be get the calculated value in a variable and evaluate in the if block.

int prime= prime(num);

//see if the returned value is None(not prime) or not None(prime)
if (prime == 1){
    System.out.println("It is a prime");
}

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