d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Basic Programming Question.
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 19 2013 05:41pm
I'm trying to code it so it asks the question "What is the power of (number) raised to the power of (2)"
**** this is a intro to programming class *****
I set the power to = 2, but for some reason, it gives me like 16.0 or 25.0; here is my code..


power is set as an int
number is set as an int

System.out.println(" You are now veiwing a sample test question");
number = (int)(Math.random() * 10);
power = Math.pow(number, 2);
System.out.println("What is the answer of the number " + number + (" raised to the power of ") + power);

if (number * power == yourAnswer)
{
System.out.println(" You are correct!");
}
else
{
System.out.println("You are wrong!");
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 19 2013 05:57pm
im really not sure what your problem is. your explanation isn't very helpful

i will say this looks fishy though

if (number * power == yourAnswer)


suppose the number is 8. we know 8 squared = 64, but you'll say they're wrong because you're checking 8*2=16
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Mar 19 2013 08:28pm
Quote (carteblanche @ Mar 19 2013 06:57pm)
im really not sure what your problem is. your explanation isn't very helpful

i will say this looks fishy though

if (number * power == yourAnswer)


suppose the number is 8. we know 8 squared = 64, but you'll say they're wrong because you're checking 8*2=16


I think he's asking the user the find the answer to a randomly found integer to the second power

His code is on a whole other level of dumb though
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 19 2013 08:40pm
from the java website:

Code
static double pow(double a, double b)
          Returns the value of the first argument raised to the power of the second argument.



(assuming your problem was why it had the trailing .0)

This post was edited by Eep on Mar 19 2013 08:42pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 19 2013 08:54pm
Quote (Trig @ Mar 19 2013 07:41pm)
I'm trying to code it so it asks the question "What is the power of (number) raised to the power of (2)"
**** this is a intro to programming class *****
I set the power to = 2, but for some reason, it gives me like 16.0 or 25.0; here is my code..


power is set as an int
number is set as an int

System.out.println(" You are now veiwing a sample test question");
  number = (int)(Math.random() * 10);
  power = Math.pow(number, 2);
  System.out.println("What is the answer of the number " + number + (" raised to the power of ") + power);
 
  if (number * power == yourAnswer)
  {
    System.out.println(" You are correct!");
  }
  else
  {
    System.out.println("You are wrong!");
  }


multiple flaws mixed with unclear questions is no way to get help, so i will be taking stabs in the dark.

if you want to remove your decimal places cast your answer as (int), although if this were C (which its not) the compiler should have yelled at you that you are supplying ints to a double function without a cast.

as for your if statement there are two ways to fix this. one, you can simply use the exp() or pow() functions located in your math class, or two, loop power times and during each loop multiple the number by itself as so.

Code
int answer;
for(int i = 0; i < power; i++)
{
    answer *= number
}
if(answer == youranswer)
{
  //correct
}else {
  //incorrect
}


simplest way is to use the pow function inside the if statement like so.

Code
if((int)Math.pow(number, power) == youranswer)


This post was edited by AbDuCt on Mar 19 2013 08:56pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 20 2013 07:36am
Fixed it for you

Code
import java.util.Random;
import java.util.Scanner;

public class Main {
 public static void main( String[] args ) {

   while ( true ) {
     System.out.println( "You are now veiwing a sample test question" );
     int number = new Random( ).nextInt( 10 );
     int power = (int) Math.pow( number, 2 );
     System.out.println( "What is the answer of the number " + number
         + (" raised to the power of ") + 2 );
     if ( power == new Scanner( System.in ).nextInt( ) ) {
       System.out.println( " You are correct!" );
     } else {
       System.out.println( "You are wrong! The answer is " +  power );
     }
   }
 }
}


This post was edited by labatymo on Mar 20 2013 07:38am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll