Author |
Message |
Nikola
|
Posted: Sat Mar 07, 2009 7:15 pm Post subject: 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);
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Euphoracle
|
Posted: Sat Mar 07, 2009 7:32 pm Post subject: RE:Faulty Loop! HELP! |
|
|
Why not just set it to 1?
int_num = 1; |
|
|
|
|
|
Nikola
|
Posted: Sat Mar 07, 2009 7:49 pm Post subject: 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
|
Posted: Sat Mar 07, 2009 10:26 pm Post subject: RE:Faulty Loop! HELP! |
|
|
int_num -= (25 * (int)(int_num / 25))
? |
|
|
|
|
|
Nikola
|
Posted: Sat Mar 07, 2009 10:45 pm Post subject: 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
|
Posted: Sun Mar 08, 2009 2:27 am Post subject: RE:Faulty Loop! HELP! |
|
|
Turing: |
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
|
|
|
|
|
|
Nikola
|
Posted: Sun Mar 08, 2009 11:35 am Post subject: RE:Faulty Loop! HELP! |
|
|
i dont understand why would i need it in turing? |
|
|
|
|
|
andrew.
|
Posted: Sun Mar 08, 2009 11:47 am Post subject: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Ktomislav
|
Posted: Sun Mar 08, 2009 5:38 pm Post subject: Re: Faulty Loop! HELP! |
|
|
Why not just use
or
I don't know which is right. |
|
|
|
|
|
Euphoracle
|
Posted: Sun Mar 08, 2009 7:54 pm Post subject: Re: Faulty Loop! HELP! |
|
|
Ktomislav @ Sun Mar 08, 2009 5:38 pm wrote: Why not just use
or
I don't know which is right.
He said he wanted to subtract
The second one is right for java, first one for turing XD |
|
|
|
|
|
Dark
|
Posted: Thu Mar 12, 2009 8:24 pm Post subject: Re: Faulty Loop! HELP! |
|
|
Java: |
int num; ...//Blah blah get input...
if (num > 25)
{
for (int i = (num-25); i < num; num--)
{
//woohoo
}
}
|
|
|
|
|
|
|
|