Reply
Old 02-08-2012, 07:36 PM   #1 (permalink)
 
Status: Senior Member
Posts: 151,118



Default I need help with some java homework, I have some code already!!?

Question 1 ) Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2 , the message "player1 wins" is printed to standard out. When score2 exceeds score1 , the message "player2 wins" is printed to standard out. In each case, the variables player1Wins, , player1Losses , player2Wins, and player2Losses, , are incremented when appropriate.

Finally, in the event of a tie, the message "tie" is printed and the variable tieCount is incremented.

MY CODE:

if (score1>score2)
{
System.out.println("player1 wins");
}
else if (score1<score2)
{
System.out.println("player2 wins");
}
else if (score1==score2)
{
System.out.println("tie" + tieCount);
}

BUT IT"S INCORRECT>><<

Question 2 ) Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books.

Write a statement that assigns freeBooks the appropriate value based on the values of the boolean variable isPremiumCustomer and the int variable nbooksPurchased .


MY CODE:

if (isPremiumCustomer == true)
{
if (nbooksPurchased >= 5)
{
freeBooks == 2;
{
if (nbooksPurchased >= 8)
{
freeBooks == 1;
}
}
}
}
else if (isPremiumCustomer == false)
{
if (nbooksPurchased >= 7)
{
freeBooks == 1;
{
if (nbooksPurchased >= 12)
{
freeBooks == 2;
}
}
}
}

BUT ITS INCORRECT >><<

Question 3 ) Write a for loop that prints the integers 50 through 1, separated by spaces. Use no variables other than count .

Question 4 ) Write a statement that terminates the current loop when the value of the int variables x . and y .are equal

Question 5 ) Suppose that the code below is the body of some loop. Use a continue statement to make sure that nothing is written to standard out when y is 0.



MY CODE:
int x = stdin.nextInt();
int y = stdin.nextInt();
System.out.println(x / y);

BUT IT'S INCORRECT>><<


THANK YOU IN ADVANCE FOR HELP!
Unknown_User is offline   Reply With Quote
Old 02-08-2012, 07:37 PM   #2 (permalink)
 
Status: Junior Member
Posts: 4



Default I need help with some java homework, I have some code already!!?

OK So the point of Yahoo! Answers and the community here isn't to do your homework, but to guide you on the right path.
So I'll just let you know what you're doing wrong on a few parts.

For the first question, for the final case where you say else if (score1 == score2), use an else statement instead, it just simplifies the code.
When player1 wins, you need to increment a variable called player1wins and increment player2losses
When player 2 wins, you do the opposite (increment player2 wins and player1losses)

For the next two questions, I think you should just sit and think about all the possibilites of the different variables. Programming is simply logic; if you can think it out in your head or verbally (or just the logic on paper) writing it in code becomes TONNES easier.

Hope this helps!
Sam0316 is offline   Reply With Quote
more..

Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with Java Homework? Micah PHP Code/ PHP Codes 1 10-09-2013 03:05 AM
java-homework help! got the logic but don't know how to put into java code words? freky8ss Coding 0 05-02-2013 03:06 PM
Java Homework please help me fix the code i'm so tired of searching.? Keysean Coding 1 04-15-2013 11:03 PM
How can I wirte this code for my java homework lorassult Coding 2 09-28-2011 02:39 AM
Help on java homework Preerfels Billing Coding 1 05-11-2011 01:36 PM