Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help, please. I'm lost

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help, please. I'm lost

    So this is what I have so far
    public class lab3 {
     
    import java.util.Scanner;
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
     
    System.out.println(" ********************* ");
    System.out.println(" * Math Questions * ");
    System.out.println(" ********************* ");
    System.out.println("");
    Scanner in = new Scanner(System.in);
    System.out.print(" Please enter an integer. ");
    int int1 = in.nextInt();
    System.out.print(" Please enter another integer. ");
    int int2 = in.nextInt();
    System.out.println("");
    System.out.println(" Answer the following questions: ");
    System.out.println("");
     
    }
     
    }

    This is what I need my code to do-

    Mibpaste.bin (click me!)

    Any help would be appreciated, I'm just not sure what to do next


    Thanks
    Last edited by Borb; October 17th, 2010 at 06:04 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Help, please. I'm lost

    Errors: ( Fix in this order )


    #1: The
    import java.util.Scanner;
    Must go before the
     public class lab3 {

    #2: In your bin sheet it uses the dash -, not the star *. Try editing.
    System.out.println(" ********************* ");
    into
    System.out.println("----------------------");
    on both of the ***.

    Also, take the * before and after the Math Questions printout, so it is more accurately
    copying what you want.


    Help:
    #1: You understand the System.outprintln(""); Idea, so for the first line that you want to

    print out:
    1. 5 * 3 = ?

    Which is the first integer times the second integer, To put a variable in that, outside of

    quotations you put the variable set to the number, in this case the first variable is int1,

    when making another part inside a System.out.print(); method, you must add a plus

    sign(+), than you want it to show the *, so you put quotation marks (Or tell it to type

    something) * with a space before an after. than a plus sign again, and the second integer,

    or int2. Note this doesn't have to be an integer. In the example:

    class example {
     
      public static void main(String[] args) {
     
    String teststring = "This will output on the screen.";
    System.out.println(teststring);
    }
    }

    It will put whatever teststring is set to, which in this case is "This will output on the screen."

    it WILL require the "" in setting the variable because it puts exactly whats on teststring

    into it.

    So, following what I just told you, to put

    1. 5 * 3 = ?
    on the screen you need to put the following :

    System.out.println("1. " + int1 + " * " + int2);

    Note the "1. " to start it, this writes it like you want it too, given what you told me.

    #2:
    Now that it print that out, your going to want it too scan that, and see if it's write.
    For this we usually would want a double, but the site linked did not have accuracy of a

    double, so we are going to use an integer again.

    Our next line will be:

    answer = int1*int2;

    Than since we want to scan the line:
    int response = in.nextInt();

    Now we are stuck in another problem: How do we make the program know if it's write or

    wrong! It's time for an if statement!

    We know conceptually that if the answer is int1*int2 it is correct, and if it does not equal

    that, it is wrong.

    An if statement, explained here:

    if/else Statement | Example Depot

    should have 2 numbers, compare, than contrast.

    So if you read that guide well, than you should know the code for this is going to look

    something like

    if(answer==response)
    {
    System.out.println("Correct.");
    }

    But wait, when we get to the end of the code, it needs to tell you the percent correct!

    There will be 3 questions, so we don't need a variable for that, but we need one for

    amount of correct. So at the top of the program, after you began the program add.

    int correct;
    So it looks like

    public static void main(String[] args) {
    // TODO Auto-generated method stub
      int correct = 0;

    and than the rest of your program so far.

    Than in your if statement, make

    if(answer==response)
    {
    System.out.println("Correct.");
    correct = correct+1;
    }

    But what if it's wrong?

    Remember, it's an if-else statement, not just an if statement.

    So we want

    if(answer==response)
    {
    System.out.println("Correct.");
    correct = correct+1;
    }
    else
    {
    System.out.println("Wrong!");
    }


    Use that method through the other 2 problems, (I will give the full program at the end);

    #3:
    Now we are done with those parts, but the last part where it should say:

    You did OK.
    You got 2 correct.
    That's 66.66667%
    (if you had two correct)

    Than you should use a few more if statements.

    So far we have :

    import java.util.Scanner;
    public class lab3 {
     
     
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
      int correct = 0;
     
    System.out.println(" ---------------------- ");
    System.out.println("Math Questions");
    System.out.println(" ---------------------- ");
    System.out.println("");
    Scanner in = new Scanner(System.in);
    System.out.print(" Please enter an integer. ");
    int int1 = in.nextInt();
    System.out.print(" Please enter another integer. ");
    int int2 = in.nextInt();
    System.out.println("");
    System.out.println(" Answer the following questions: ");
    System.out.println("");
    System.out.println("1. " + int1 + " * " + int2);
    int response = in.nextInt();
    int answer = int1*int2;
    if(answer==response)
    {
    System.out.println("Correct.");
    correct = correct+1;
    }
    else
    {
    System.out.println("Wrong!");
    }
     
     
    System.out.println("2. " + int1 + " / "  + int2);
    answer = int1/int2;
     
     
    if(answer==response) 
    {
    System.out.println("Correct.");
    correct = correct+1;
    }
    else
    {
    System.out.println("Wrong!");
    }
     
     
    System.out.println("3. " + int1 + " % " + int2);
    answer = int1%int2;
     
    if(answer==response) 
    {
    System.out.println("Correct.");
    correct = correct+1;
    }
    else
    {
    System.out.println("Wrong!");
    }
     
     
     
     
     
    }
     
    }

    if you have followed what I said.

    Now for the last part


    So, first of we need a String for how well you did (bad ok perfect), and a double for the %

    correct, because we already have the amount correct.

    So, to get the ok, lets add a new string after all that we have, with that if statement!
    Of course, we are going to need 3 of them. ( if 0, than terrible, 1, than bad, if 2 than ok, if

    3 than perfect). And we are going to need to initialize the statement before the ifs, by

    adding

    String well = "perfect.";

     
    String well = "OK";
    if(correct==0)
    {
    well = "very bad.";
    }
    if(correct==1)
    {
    well = "poorly.";
    }
    if(correct==2)
    {
    well = "OK.";
    }
    3 is a special scenario, so we'll put that in with an if statements.

    Note the periods and exclamationpoints, they are vital!

    And now for the % correct, this one is simple, again using if statements.

    double percentcorrect = 100;
    if(correct==0)
    {
      percentcorrect = 0.0;
    }
    if(correct==1)
    {
      percentcorrect = 33.33334;
    }
    if(correct==2)
    {
      percentcorrect = 66.66667;
    }
    And that's all we need for the end.

    Now just implement the System.out.println(); and we have the program and the if

    statement for the 3 correct and were done!

    (If you don't know how add

    if(correct!=3)
    {
    System.out.println("You did " + well);
    System.out.println("You got " + correct + " correct.");
    System.out.println("That's " + percentcorrect + "%");
    }
    else
    {
    System.out.println("You did " + well);
    System.out.println("Good job!");
    System.out.println("That's " + percentcorrect + "%");
    )
    And here is our finished program!

    import java.util.Scanner;
    public class lab3 {
     
     
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
      int correct = 0;
     
    System.out.println(" ---------------------- ");
    System.out.println("Math Questions");
    System.out.println(" ---------------------- ");
    System.out.println("");
    Scanner in = new Scanner(System.in);
    System.out.print(" Please enter an integer. ");
    int int1 = in.nextInt();
    System.out.print(" Please enter another integer. ");
    int int2 = in.nextInt();
    System.out.println("");
    System.out.println(" Answer the following questions: ");
    System.out.println("");
    System.out.println("1. " + int1 + " * " + int2 + " = ?");
    int response = in.nextInt();
    int answer = int1*int2;
    if(answer==response)
    {
    System.out.println("Correct.");
    correct = correct+1;
    System.out.println("");
    }
    else
    {
    System.out.println("Wrong!");
    System.out.println("");
    }
     
     
    System.out.println("2. " + int1 + " / "  + int2 + " = ?");
    answer = int1/int2;
    response=in.nextInt();
     
     
    if(answer==response) 
    {
    System.out.println("Correct.");
    correct = correct+1;
    System.out.println("");
    }
    else
    {
    System.out.println("Wrong!");
    System.out.println("");
    }
     
     
    System.out.println("3. " + int1 + " % " + int2 + " = ?");
    response = in.nextInt();
    answer = int1%int2;
     
    if(answer==response) 
    {
    System.out.println("Correct.");
    correct = correct+1;
    System.out.println("");
    }
    else
    {
    System.out.println("Wrong!");
    System.out.println("");
    }
     
     
    String well = "perfect.";
    if(correct==0)
    {
    well = "terrible.";
    }
    if(correct==1)
    {
    well = "bad.";
    }
    if(correct==2)
    {
    well = "OK.";
    }
    double percentcorrect = 100;
    if(correct==0)
    {
      percentcorrect = 0.0;
    }
    if(correct==1)
    {
      percentcorrect = 33.33334;
    }
    if(correct==2)
    {
      percentcorrect = 66.66667;
    }
     
     
     
    if(correct!=3)
    {
    System.out.println("You did " + well);
    System.out.println("You got " + correct + " correct.");
    System.out.println("That's " + percentcorrect + "%");
    }
    else
    {
    System.out.println("Good job!");
    System.out.println("You got 3 correct.");
    System.out.println("That's " + percentcorrect + "%");
    }
     
     
     
    }
     
    }


    NOTE::

    I know there is a better way for the part where you get 66.66667 if 2 are correct, but I tend to get loss of precision when I do 3/2, so instead of adding Bigdecimals and confusing things, this will do for this program. Might want to look into that though, because your teacher might not like this way.

    All variable names you can change to your will!
    Please READ IT ALL, do not just copy the end code, if you see errors (which I'm sure it has it's fill), or thought of a better way to do it, say so, I'm learning to
    Last edited by Tjstretch; October 17th, 2010 at 08:53 PM.

Similar Threads

  1. New to this..so lost trying to learn...
    By ryan1234 in forum Object Oriented Programming
    Replies: 5
    Last Post: September 21st, 2010, 05:35 PM
  2. I'm lost
    By stoptheerrors in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2010, 08:47 AM
  3. Im lost....
    By brale76578 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 31st, 2010, 08:09 AM
  4. lost
    By nyny in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2010, 07:32 AM
  5. Need Help! I'm completly lost!
    By DeMolay8613 in forum Java Theory & Questions
    Replies: 0
    Last Post: January 11th, 2010, 12:21 PM