A
A
Alexey Medvedev2015-08-03 07:25:26
Java
Alexey Medvedev, 2015-08-03 07:25:26

Is it possible to give an int variable ANY integer value, for example from a span (C++ && Java)?

int number, i, N;
if (i == 0 || i == (number * N)) {...} 
if (i == 1 || i == (1 + number * N)) {...} 
if (i == 2 || i == (2 + number * N)) {...} 
/*
мне надо, чтобы N могла равняться любому целому числу одновременно (!)
в математике это можно, это легко, а можно ли так сделать в Java? Ну, или можно пример на C++?
*/

It's just that there is a certain algorithm that will be correct and should work for any integer value of N.
UPD
It will take longer to describe why I need such a rigmarole, so I will say the following. I could also write it like this:
if (i == 0 || i == (number) || i== (number * 2) || i == (number * 3)) {...}  //и так далее
if (i == 1 || i == (1 + number) || i== (1 + number * 2) || i == (1 + number * 3)) {...}  //и так далее

But my interval goes from 0 to hundreds, you know, not that option.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AxisPod, 2015-08-03
@RockBearLTD

You don’t need to get into programming, apparently, apparently you didn’t hear about the remainder when dividing in elementary school (if we talk about mathematics)

//if (i == 0 || i == (number) || i== (number * 2) || i == (number * 3)) {...}  //и так далее
if(i == 0 || i % number == 0) {...}
//if (i == 1 || i == (1 + number) || i== (1 + number * 2) || i == (1 + number * 3)) {...}
if (i == 1 || i % number == 1) {...}
// Ну и на последок
int mod = i % number;

And do what you need with mod, it will store your number.
PS Well, yes, checks i==0, i==1 are not needed, apparently I myself was on a roll.

L
LittleFatNinja, 2015-08-03
@LittleFatNinja

Is it possible to give an int variable ANY integer value

No

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question