Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

foebotting

would appreciate some help, math

9 posts in this topic

So I'm supposed to make this into a code. Where the x stands for a number that you type in when you execute the script. z is the number you think it is, if you type in correct answer it give you "correct answer because 37 + 16 is 53.


 


 


type in two numbers: x x


what is x + x? z


Correct Answer!


 


for example:


 


type in two numbers: 37 16


what is 37 + 16? 53


Correct Answer!


 


_______________


edit:


I have another one but this is with "wrong answer".


type in two numbers: 37 16


what is 37 + 16? 58


Wrong answer! Correct answer is 53.


Share this post


Link to post
Share on other sites

OK so what is your question.Because by the look of it all you are doing is adding 2 numbers and comparing the result to z ^^.You should not have any math problems here, so I'm assuming your having trouble translating it into code?.

Share this post


Link to post
Share on other sites

Pseudo code:

 

print("type in two numbers: ");

num1 = nextInt;

num2 = nextInt;

answer = num1 + num2;

print("what is " + num1 + " + " + num2 + "? ");

guess = nextInt;

if (guess == answer)

    println("Correct Answer!");

else

    println("Wrong answer! Correct answer is " + answer + ".");

1 person likes this

Share this post


Link to post
Share on other sites

@Encoded aaaa he's prob a first year and this was his homework ;).You gave away the answer too easily:b

That code doesn't compile, he still has to convert it to valid Java and since it is such an easy problem he should understand the logic just from doing that.

Share this post


Link to post
Share on other sites

@Encoded

 

@Red_Spark

 

 

Thanks Encoded, yeah I notice it. Well I've finished it myself, does it look clean?

 

 

 

while(true){
        int realA = 0;
                                   
        System.out.print("enter two numbers:");
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();
        scanner.nextLine();
        System.out.print("enter a way to calc:");
        String type = scanner.nextLine();
        if (type.equals("+"))
        {
        System.out.print("what is " + num1 + "+" + num2 + ": ");
         realA = num1 + num2;
        }
        else if (type.equals("-"))
         {
         System.out.print("what is" + num1 + "-" + num2 + ": ");
         realA = num1 - num2;
         }
         else if (type.equals("*"))
         {
                 System.out.print("what is " + num1 + "*" + num2 + ": ");
         realA = num1 * num2;
         }
         else if (type.equals("/"))
         {
                 System.out.print("what is " + num1 + "/" + num2 + ": ");
         realA = num1 / num2;
         }
                                               
                                   
 int sum = scanner.nextInt();
                                               
                                               
                                               
if(realA == sum )
 {
 System.out.println("Correct Answer!");
                                                       
                                                       
}
 else
         {
         System.out.println("Incorrect" + " the answer is " + realA);
         }
 System.out.print("do you want to continue? (yes/no)? ");
 if(scanner.next().equals("no")) break;
  System.out.println();
                                        }
                    }
               
        }
Edited by foebotting

Share this post


Link to post
Share on other sites

Looks ok to me :).Consider using equalsIgnoreCase() and trim() to fix user input mistakes.(Just a good habit to get into)

 

PS: int sum = scanner.nextInt(); You should prob have a System.out.print before it, to ask the user for what he thinks the snswer is.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.