Jump to content

Java: How to Count odd Integers?

Tocsin_786
Go to solution Solved by Cela1,
32 minutes ago, Tocsin_786 said:

Yea its a conditional statement, kinda like an If statement but condensed. So in this case the n input is either being subtracted or added by either 1 or 2. How would I change it so it does it in intervals automatically? like what if I want to count every second odd number or something like that? 

I'm not *quite* sure what you mean, but if you change the -1 / -2 to a +3 / +5, then you should get the second odd number. If you actually want to count, then use a FOR loop like;

 

import java.util.Scanner;
public class OddPredecessor {
	public static void main(String [] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
                n = (n-1 % 2==1) ? n -1 : n -2
                for (int i = n; i < y; i+=2) {
                       System.out.println(n); 
                }
	}
}

 

Where 'y' is where you want to stop (can be written as n+a, where a is the amount over the selected digit that you want to show).

This has been puzzling me for a month. All my HW is due on codelab and so it has to be very specific in what its asking. I dont conceptually understand how I can have a user input a number, and then have Java count up or down to the next odd number... or just count in odds or even in general. The code I have provided is from Chegg, but they failed to explain anything, and so I'm lost. 

* keep in mind, I can't use arrays, or if statements because we havent learned that yet.*

 

Heres the code so you can copy and paste. 

import java.util.Scanner;
public class OddPredecessor {
	public static void main(String [] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		System.out.println((n-1 % 2==1) ? n -1 : n -2);
	}
}

 

1697351192_Screenshot2021-03-06221642.thumb.png.f2121fae284b7f702d9f49efdf6545e0.png

 

804044442_Screenshot2021-03-06221708.thumb.png.5e17d4e3d3ea674c276960222d3e4307.png

 

 

 

Space Journal #1: So Apparently i  was dropped on the moon like i'm a mars rover, in a matter of hours i have found the transformers on the dark side of the moon. Turns out its not that dark since dem robots are filled with lights, i waved hi to the Russians on the space station, turns out all those stories about space finding humans instead of the other way around is true(soviet Russia joke). They threw me some Heineken beer and I've been sitting staring at the people of this forum and earth since. 

Link to comment
Share on other sites

Link to post
Share on other sites

In Java (and many other languages) % is the modulo operator (you can think of it like a remainder operator - except it does things slightly differently with negative numbers).  The '?'  is a ternary operator, meaning that if the modulo operation is true it will select the second (n-2) branch, otherwise it will select the (n-1) branch.

 

Edit: Specifically 'n' is being read as the next integer (IDK why they are doing it like that), so 'n-1' represents the number you typed in. Using modulo 2, if you type in an even number you get 0 (even - first) out, so it subtracts one to get the last odd number,. Otherwise that number will be even, so we subtract 2 to get the last even number.

 

Hope this helps. Feel free to ask more if it doesn't.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Cela1 said:

In Java (and many other languages) % is the modulo operator (you can think of it like a remainder operator - except it does things slightly differently with negative numbers).  The '?'  is a ternary operator, meaning that if the modulo operation is true it will select the second (n-2) branch, otherwise it will select the (n-1) branch.

Yea its a conditional statement, kinda like an If statement but condensed. So in this case the n input is either being subtracted or added by either 1 or 2. How would I change it so it does it in intervals automatically? like what if I want to count every second odd number or something like that? 

Space Journal #1: So Apparently i  was dropped on the moon like i'm a mars rover, in a matter of hours i have found the transformers on the dark side of the moon. Turns out its not that dark since dem robots are filled with lights, i waved hi to the Russians on the space station, turns out all those stories about space finding humans instead of the other way around is true(soviet Russia joke). They threw me some Heineken beer and I've been sitting staring at the people of this forum and earth since. 

Link to comment
Share on other sites

Link to post
Share on other sites

32 minutes ago, Tocsin_786 said:

Yea its a conditional statement, kinda like an If statement but condensed. So in this case the n input is either being subtracted or added by either 1 or 2. How would I change it so it does it in intervals automatically? like what if I want to count every second odd number or something like that? 

I'm not *quite* sure what you mean, but if you change the -1 / -2 to a +3 / +5, then you should get the second odd number. If you actually want to count, then use a FOR loop like;

 

import java.util.Scanner;
public class OddPredecessor {
	public static void main(String [] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
                n = (n-1 % 2==1) ? n -1 : n -2
                for (int i = n; i < y; i+=2) {
                       System.out.println(n); 
                }
	}
}

 

Where 'y' is where you want to stop (can be written as n+a, where a is the amount over the selected digit that you want to show).

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×