Looking for help with a couple of codes
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Looking for help with a couple of codes

  1. #1
    Join Date
    Sep 2011
    Posts
    7

    Looking for help with a couple of codes

    Hello guys, I am new to the site and currently learning java. I am working on a couple of codes that I cannot seem to get to run correctly. These are both for a school assignment. I want to be clear that I am not asking for anyone to do the assignment for me. I have completed most of the code work already, there are just two problems with these that I can figure out. Any help would be appreciated!

  2. #2
    Join Date
    Sep 2011
    Posts
    7

    Re: Looking for help with a couple of codes

    The first code is to calculate MPG. I am able to get the first set of numbers to calculate but I am supposed to be able to enter a new set that adds or subtracts from the total. After the first time through the loop, the mpgtank and totalmpg should be different, and its not changing:


    import java.util.Scanner;
    public class Lab5a {
    public static void main( String args[] ) {

    Scanner input = new Scanner( System.in );

    double milesdriven = 1.1;
    double gallonsused = 1.1;
    double mpgtank = 1.1;
    double totalmpg = 1.1;


    System.out.print("Enter miles since tank was full (-999 to quit) ");
    milesdriven = input.nextDouble();
    while(milesdriven!=-999)
    {

    System.out.print( "Enter gallons used in refilling tank:" );
    gallonsused = input.nextDouble();

    mpgtank = milesdriven / gallonsused;
    totalmpg = milesdriven / gallonsused;

    System.out.printf( "MPG this tankful: %1.2f ", mpgtank );

    System.out.println();

    System.out.printf("Total MPG: %1.2f", totalmpg);

    System.out.println();

    System.out.print("Enter miles since tank was full (-999 to quit) ");
    milesdriven = input.nextDouble();
    {

    System.out.print( "Enter gallons used in refilling tank:" );
    gallonsused = input.nextDouble();

    mpgtank = milesdriven / gallonsused;
    totalmpg = milesdriven / gallonsused;

    System.out.printf( "MPG this tankful: %1.2f ", mpgtank );

    System.out.println();

    System.out.printf("Total MPG: %1.2f", totalmpg);

    System.out.println();

    System.out.print("Enter miles since tank was full (-999 to quit) ");
    milesdriven = input.nextDouble();
    while(milesdriven!=-999)
    {

    System.out.print( "Enter gallons used in refilling tank:" );
    gallonsused = input.nextDouble();

    mpgtank = milesdriven / gallonsused;
    totalmpg = milesdriven / gallonsused;

    System.out.printf( "MPG this tankful: %1.2f ", mpgtank );

    System.out.println();

    System.out.printf("Total MPG: %1.2f", totalmpg);

    System.out.println();

    System.out.println("Press any key to continue . . .");

    }
    }}}}

  3. #3
    Join Date
    Sep 2011
    Posts
    7

    Re: Looking for help with a couple of codes

    The second code is a easter egg hunt. This code is complete except I have to put in the ability for the user to input "Q" to end the game. I tried to follow the same method I did with the MPG code, but I can't seem to figure out where I can input the "while" line.

    import java.util.Scanner;

    public class EggHunt{

    public static void main(String args[]){

    Scanner input = new Scanner( System.in );

    String gender;
    String type;
    int countboys=0;
    int countgirls=0;
    int point=0;

    System.out.println("Enter gender of competitor: ");
    gender = input.next();

    System.out.println("Enter egg type: A, B, or C");
    type = input.next();

    if(type.equalsIgnoreCase("A"))
    point=5;
    else if(type.equalsIgnoreCase("B"))
    point=10;
    else
    point=30;

    if(gender.equalsIgnoreCase("M"))
    countboys+=point;
    else
    countgirls+=point;

    System.out.println("Enter gender of competitor: ");
    gender = input.next();

    System.out.println("Enter egg type: A, B, or C");
    type = input.next();

    if(type.equalsIgnoreCase("A"))
    point=5;
    else if(type.equalsIgnoreCase("B"))
    point=10;
    else
    point=30;

    if(gender.equalsIgnoreCase("G"))
    countgirls+=point;
    else
    countboys+=point;

    System.out.println();

    System.out.println("The boys scored "+countboys+" and girls scored "+ countgirls);
    if(countboys>countgirls)
    System.out.println("Boys are the champions");
    else if(countgirls>countboys)
    System.out.println("Girls are the champions");
    else
    System.out.println("There is a tie between girls and boys");
    } }

  4. #4
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,440

    Re: Looking for help with a couple of codes

    I don't see where ask the user or test his response for the 'Q'
    I only see this question: Enter egg type: A, B, or C
    Norm

  5. #5
    Join Date
    Sep 2011
    Posts
    7

    Re: Looking for help with a couple of codes

    I know, thats where I am lost, where would I put in the line to quit the race and execute it?

  6. #6
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,440

    Re: Looking for help with a couple of codes

    I'd put it where I wanted the race to quit.

    At what point in the program could the user be asked if he wanted to quit?
    Norm

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Looking for help with a couple of codes

    Please use code tags when posting code.

    I am able to get the first set of numbers to calculate but I am supposed to be able to enter a new set that adds or subtracts from the total.
    Why have you repeated the same code twice? The reason for using loops is they save you from having to repeat the same code over and over again.

    I suggest you sit down with a pen and paper and write down the questions you need to ask the user and then look at which questions you will need to ask again and again until the user wants to exit. Then put these questions and associated code inside a single loop.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,440

    Re: Looking for help with a couple of codes

    Norm

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Looking for help with a couple of codes

    Thanks for the warning Norm.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Sep 2011
    Posts
    7

    Re: Looking for help with a couple of codes

    Norm, I would put it after the first set of totals, boys total and girls total. You are supposed to input Q at anytime to terminate to program, no matter if its asking you gender or egg type, thats where I am lost

  11. #11
    Join Date
    Sep 2011
    Posts
    7

    Re: Looking for help with a couple of codes

    Quote Originally Posted by Norm View Post
    is this a problem?

    sorry I was just asking for input from different sources.

  12. #12
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,440

    Re: Looking for help with a couple of codes

    You are supposed to input Q at anytime to terminate to program, no matter if its asking
    It all depends on what the instructor wants,
    or what will be the less confusing or easiest for the user.
    And of course it is up to the programmer to decide how often to ask for and test for the "Q"
    Norm

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Looking for help with a couple of codes

    @djluckycharm If you post the same question on multiple forums without stating in your post that you have posted the question elsewhere you run the risk of upsetting all those that would otherwise help you.

    I suggest you decide which forum you want to use and close down the question on all the other forums you have posted on preferably by providing a link, in each of the threads you are closing, to the forum you are keeping open.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Looking for help with a couple of codes

    is this a problem?

    sorry I was just asking for input from different sources.
    Yes it is a problem. We all give our time for free and don't like wasting it answering a question that has already been answered elsewhere.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  15. #15
    Join Date
    Sep 2011
    Posts
    7

    Re: Looking for help with a couple of codes

    well i do apologize then...

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center