d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Basic Java Question
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 26 2013 06:51pm
Fixed it..

This post was edited by Trig on Mar 26 2013 06:54pm
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 28 2013 06:33pm
Another Question on loops...


I have this peice of my program that is supposed to show 5 sample test questions, and when i give the answer, it either tells me im wrong or right, and after the 5th one the loop should end..

here is my code;
number = (int)(Math.random() * 10);
power = Math.pow(number, 2);

System.out.println(" You are now veiwing 5 sample test question");

for (int j = 0 * 10; j < 5;)
{
System.out.println("What is the answer of the number " + number + (" raised to the power of 2 "));
power = input.nextInt();
}



yourAnswer = input.nextDouble();

if (power == yourAnswer)
{
System.out.println(" You are correct!");
}
else
{
System.out.println("You are wrong!");
}




here is the sample out put im getting...


Candidate Name:
Jeff
What is test score:
98
Enter 1 to select employment catagory
Enter 2 to select interview possibility
Enter 3 to view a sample test question
3
You are now veiwing 5 sample test question
What is the answer of the number 0 raised to the power of 2
0
What is the answer of the number 0 raised to the power of 2
12
What is the answer of the number 0 raised to the power of 2
32
What is the answer of the number 0 raised to the power of 2
34
What is the answer of the number 0 raised to the power of 2
2
What is the answer of the number 0 raised to the power of 2




the code has to be a for loop for me to get credit..
i dont know if i should have j in there as the variable or if i should have my defined variable 'number' in there

if you need additional info, let me know.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 28 2013 06:45pm
Quote (Trig @ Mar 28 2013 08:33pm)
Another Question on loops...


I have this peice of my program that is supposed to show 5 sample test questions, and when i give the answer, it either tells me im wrong or right, and after the 5th one the loop should end..

here is my code;
number = (int)(Math.random() * 10);
  power = Math.pow(number, 2);
 
  System.out.println(" You are now veiwing 5 sample test question");
   
  for (int j = 0 * 10; j < 5;)
    {
      System.out.println("What is the answer of the number " + number + (" raised to the power of 2 "));
      power = input.nextInt();
    }
   
   
     
  yourAnswer = input.nextDouble();
 
  if (power == yourAnswer)
  {
    System.out.println(" You are correct!");
  }
  else
  {
    System.out.println("You are wrong!");
  }




here is the sample out put im getting...


Candidate Name:
Jeff
What is test score:
98
Enter 1 to select employment catagory
Enter 2 to select interview possibility
Enter 3 to view a sample test question
3
You are now veiwing 5 sample test question
What is the answer of the number 0 raised to the power of 2
0
What is the answer of the number 0 raised to the power of 2
12
What is the answer of the number 0 raised to the power of 2
32
What is the answer of the number 0 raised to the power of 2
34
What is the answer of the number 0 raised to the power of 2
2
What is the answer of the number 0 raised to the power of 2




the code has to be a for loop for me to get credit..
i dont know if i should have j in there as the variable or if i should have my defined variable 'number' in there

if you need additional info, let me know.


Please correct the following errors:
You must enter a message.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 28 2013 07:42pm
Wtf, major deja vu. I swear I just wrote someone's homework assignment with the exact same requirements as yours...
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 28 2013 07:48pm
Lmao, unless you're my proffessor? hahahha
but i still dont know how to fix my loop so that it only prints out 5, and so that it checks them as i do it,
i tried putting the if else in my loop but that doesnt work
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 28 2013 08:00pm
Quote (Trig @ Mar 28 2013 09:48pm)
Lmao, unless you're my proffessor? hahahha
but i still dont know how to fix my loop so that it only prints out 5, and so that it checks them as i do it,
i tried putting the if else in my loop but that doesnt work


I bet the other guy is in your class lol.. or maybe I am your professor, trying to catch cheaters...

and like what carteblanche pointed out. Your for loop is wrong.

Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 28 2013 08:04pm
Quote (labatymo @ Mar 28 2013 10:00pm)
I bet the other guy is in your class lol.. or maybe I am your professor, trying to catch cheaters...

and like what carteblanche pointed out. Your for loop is wrong.


okay but how do i fix it?
bc in the video, it didnt explain much and i already asked my prof so many questions, and dont want him to think i needed help with the whole thing lol..
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 29 2013 04:00pm
:/
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 30 2013 02:13pm
Quote (Trig @ Mar 28 2013 10:04pm)
okay but how do i fix it?
bc in the video, it didnt explain much and i already asked my prof so many questions, and dont want him to think i needed help with the whole thing lol..


http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll