
-----------------------------------
Nikola
Sat Mar 07, 2009 7:15 pm

Faulty Loop! HELP!
-----------------------------------
I just cant find what I'm doing wrong here. Im trying to make it so if the user inputs a number greater than 25 it will start back at 1. The problem i face though is that if someone posts 60 for example the end result is still greater than 25. So I made a loop to work untill the inputted number is less than 25 but it just wont work! Please try and evaluate the code. Thanks again!

[code]
import java.awt.*;
import hsa.Console;

public class Second
{
    static Console cs;
    public static void main (String[] args)
    {
        cs = new Console ();
        String str_word;
        int int_num, int_ctr;
      //  cs.print ("Please enter a phrase or word: ");
        //str_word = cs.readLine ();
        cs.print ("Please enter a digit: ");
        int_num = cs.readInt ();
        for (int_ctr = 0 ; int_ctr < 25 ; int_ctr++)
        {
            if (int_num > 25)
            {
                int_num = int_num - 25;
                int_ctr = int_num;
            }
            
        }
        cs.print ("Run back check: " + int_num);

    }
}
[/code]

-----------------------------------
Euphoracle
Sat Mar 07, 2009 7:32 pm

RE:Faulty Loop! HELP!
-----------------------------------
Why not just set it to 1?

int_num = 1;

-----------------------------------
Nikola
Sat Mar 07, 2009 7:49 pm

RE:Faulty Loop! HELP!
-----------------------------------
The point isnt to make it 1 if the number is greater than 25 it is to make it run back 25 digits. so if someone inputs 40 it will post 15. It subtracts each time untill the final number is 25 or less.

-----------------------------------
Euphoracle
Sat Mar 07, 2009 10:26 pm

RE:Faulty Loop! HELP!
-----------------------------------
int_num -= (25 * (int)(int_num / 25))

?

-----------------------------------
Nikola
Sat Mar 07, 2009 10:45 pm

RE:Faulty Loop! HELP!
-----------------------------------
nope still wont work i just need the loop to be fixed so it takes 25 from int_num untill int_num is 25 or less

-----------------------------------
Euphoracle
Sun Mar 08, 2009 2:27 am

RE:Faulty Loop! HELP!
-----------------------------------

var num := 0
get num

put num

put (num - (25 * (num div 25))) % Way I posted earlier

% The way you want
loop
    exit when num < 25
    num -= 25
end loop
put num

Output
[code]
40
15
15
[/code]

-----------------------------------
Nikola
Sun Mar 08, 2009 11:35 am

RE:Faulty Loop! HELP!
-----------------------------------
i dont understand why would i need it in turing?

-----------------------------------
andrew.
Sun Mar 08, 2009 11:47 am

RE:Faulty Loop! HELP!
-----------------------------------
It's just an example. Instead of posting the full solution in Java, he did it in Turing so you have to do some work.

-----------------------------------
Ktomislav
Sun Mar 08, 2009 5:38 pm

Re: Faulty Loop! HELP!
-----------------------------------
Why not just use
[code]num mod 25[/code]
or
[code]num % 25[/code]
I don't know which is right.

-----------------------------------
Euphoracle
Sun Mar 08, 2009 7:54 pm

Re: Faulty Loop! HELP!
-----------------------------------
Why not just use


He said he wanted to subtract :)

The second one is right for java, first one for turing XD

-----------------------------------
Dark
Thu Mar 12, 2009 8:24 pm

Re: Faulty Loop! HELP!
-----------------------------------

int num; ...//Blah blah get input...

if (num > 25)
{

for (int i = (num-25); i < num; num--) 
  {
//woohoo

  }

}

