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

Kamu
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.

Ashish Neupane

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, typez<=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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my sql statment return the same output multiple times

Why does my python function not return the correct times the letters are in the string?

Why does my .on() get called several times on one click

Why does my find command get executed two times?

Why does my Javascript code return object object?

Why does my while(read) loop with pipes never end?

Why does my R loop return an out of bounds error?

Webscraping in R: Why does my loop return NA?

Why does my for loop return an empty list value at the end of the list?

Why does my Javascript loop only return the first value?

Why does my for loop return 'undefined' as the last element of a string

why this way return an instance of my object correctly, but this way does not

Why does my API call function return an undefined object?

Why does my return value increase itself in an object in Java?

Why does my code return the type of the object instead of its value?

Why does my PowerShell function return Object[] instead of string[]?

Why does my picturebox image not get updated and return null?

Why does my keras LSTM model get stuck in an infinite loop?

Why does my object get overwritten in C++?

Why does my object not get inserted into std::set?

Why does my javascript object get faster and faster?

Why does my Hash not get filled when initializing with a default object

Why does my loop only read one char from my .obj file?

Why does my compiler say that my functions are defined multiple times?

Why does my for loop skip my last values in my array?

Why does my If/Else loop break out of my For loop?

my api does not return the complete object

Why does my LabView while loop appear to execute one too many times?

Why does my for loop in C++ not iterate as many times as its supposed to?