|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
| | #46 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 2,173
| Okay, I'm stuck on one part of the actual homework. Our teacher never went over arithmetic using boolean, thus I have no idea how to do it. Here's what I have (and everything works, it's just I dunno what to do next) import javax.swing.JOptionPane; public class Lab3Part2 { public static void main(String[] args) { int numberOne, numberTwo; String input, output; boolean Correct, Wrong; input = JOptionPane.showInputDialog("Enter the first integer."); numberOne = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter the second integer."); numberTwo = Integer.parseInt(input); System.out.print("Please answer the following questions: "); input = JOptionPane.showInputDialog("What is " + numberOne + "*" + numberTwo + "?"); if ( ) } } The question is: The program asks the user to enter two integers. Then it asks three questions: what's the product of the two integers, what's the quotient of the two integers, and what's the remainder of the division of the two integers. It inputs the answers to each of the question from the user, tells the user whether the answer was correct or worng, and finally prints a message that depends on how many correct answers the user provided. For example, here is a sample run of the program you will write (user inputs are in bold): ---------------------- MATH QUESTIONS ---------------------- Please enter an integer. 5 Please enter another integer. 3 Answer the following questions: 1. 5 * 3 = ? 17 Wrong! 2. 5 / 3 = ? 1 Correct. 3. 5 % 3 = ? 2 Correct. You did OK. You got 2 correct. That's 66.66667% Make sure that your program produces output exactly like the sample provided above and that it includes th epercentage of correct answers. Here are the different messages that should be printed depending on the number of correct answers: All I really want to know is how do you multiply, subtract, etc using boolean and such? Or do you not even use them for this?
__________________ Live like you'll die tomorrow, Dream like you'll live forever |
| | |
| | #48 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 2,173
| import javax.swing.JOptionPane; public class Lab3Part2 { public static void main(String[] args) { int numberOne, numberTwo; String input, output; boolean Correct, Wrong; input = JOptionPane.showInputDialog("Enter the first integer."); numberOne = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter the second integer."); numberTwo = Integer.parseInt(input); System.out.print("Please answer the following questions: "); input = JOptionPane.showInputDialog("What is " + numberOne + "*" + numberTwo + "?"); if ( numberOne *= numberTwo ) { System.out.print("Correct!"); } else { System.out.print("Wrong."); { } } I am unsure of how to bring up a dialog box that just outputs a message. When I've tried, it gives me an error message. I use input = JOptionPane.showMessageDialog(""); but, there's conflicts with strings or some shit.
__________________ Live like you'll die tomorrow, Dream like you'll live forever |
| | |
| | #49 (permalink) |
| Registered User Join Date: Jul 2002
Posts: 455
| Not trying to tell you what to do, but you should really go to the instructor after class or talk with them during class if you're having problems. I didn't do it for the first semester of programming and it really screwed me up. They're required to set apart time with students, and while there is a lot of good advice here, the teacher knows what you know about programming so far and will make it easier for you to learn. Most of the people that you are getting help from are way more advanced than you are right now, and give you advice that is way more detailed than what you need to know. It can look like plagiarism if you're not careful also. If you do get stuck and need to get help from someone, and don't understand it completely, just take it to a teacher and have them explain it to you. Read your text, pay attention in class, and programming will be easy. (at least up to the point I'm at) |
| | |
| | #50 (permalink) | |
| Fuckin' 07er Join Date: May 2007 Location: Philly
Posts: 340
| Quote:
For this: input = JOptionPane.showInputDialog("What is " + numberOne + "*" + numberTwo + "?"); if ( numberOne *= numberTwo ) { System.out.print("Correct!"); } else { System.out.print("Wrong."); { } You have numberOne *= numberTwo inside an if statement's parentheses.. That should be a TRUE or FALSE statement.. The operator "*=" is multiply and assign. Meaning you take the right side and multiply it with the left side..and then assign to left side So.. if X=5 X *= 3 X now equals 15. You need to check equality inside the parentheses, like: (numberOne == numberTwo) or (numberOne > numberTwo) or what I think you want, is.. correctAnswer = numberOne * numberTwo //store the real correct answer (input == correctAnswer) //and compare the correct answer with what the user inputs And I'm going to have to agree with the sentiment it sounds like you need to talk to your Prof about some of this stuff, it seems you're missing some of the basic/fundamental things that you'd benefit a ton from Goodluck! | |
| | |
| | #52 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 2,173
| Okay. Well, I figured it out. import java.util.Scanner; public class Lab3part3 { public static void main(String[] args) { int numberOne, numberTwo, correctAnswer; boolean True, False; Scanner keyboard = new Scanner(System.in); System.out.print("Please enter the first integer: "); numberOne = keyboard.nextInt(); System.out.print("Please enter the second integer: "); numberTwo = keyboard.nextInt(); System.out.print("What is the answer to " + numberOne + "*" + numberTwo + "?"); correctAnswer = numberOne * numberTwo; if ( correctAnswer == keyboard.nextInt() ) { System.out.println("You answered correctly!"); } else { System.out.print("Incorrect!"); } } } I think I can get the rest. BTW niteflyx, you helped me a lot. Thanks! <3
__________________ Live like you'll die tomorrow, Dream like you'll live forever |
| | |
| | #53 (permalink) |
| Fuckin' 07er Join Date: May 2007 Location: Philly
Posts: 340
| No prob, knowing when to use the different operator types can be tricky when first learning, the difference between a single and double equals sign was confusing for me when I first started :P Although I think java has some kind of value1.isEqual(value2) method.. |
| | |
| | #54 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 2,173
| now to figure out the final part...*G* import java.util.Scanner; public class Lab3part3 { public static void main(String[] args) { int numberOne, numberTwo; Scanner keyboard = new Scanner(System.in); System.out.print("Please enter the first integer: "); numberOne = keyboard.nextInt(); System.out.print("Please enter the second integer: "); numberTwo = keyboard.nextInt(); System.out.print("What is the answer to " + numberOne + "*" + numberTwo + "?"); if ((numberOne * numberTwo) == keyboard.nextInt()) { System.out.println("You answered correctly!"); } else { System.out.print("Incorrect!"); } System.out.print("What is the answer to " + numberOne + "/" + numberTwo + "?"); if ((numberOne / numberTwo) == keyboard.nextInt()) { System.out.print("You answered correctly!"); } else { System.out.print("Incorrect!"); } System.out.print("What is the answer to " + numberOne + "%" + numberTwo + "?"); if ((numberOne % numberTwo) == keyboard.nextInt()) { System.out.print("You answered correctly!"); } else { System.out.print("Incorrect!"); } } }
__________________ Live like you'll die tomorrow, Dream like you'll live forever |
| | |
| | #55 (permalink) |
| Fuckin' 07er Join Date: May 2007 Location: Philly
Posts: 340
| Just add up a counter for every 'correct' answer there is and divide by 3, then times by 100 and display that. C'mon dude, can't do all of your homework for ya :P sometimes it just takes hours of looking at the same freakin' thing and realizing.. "Oh shit...I need a semi-colon." Mixing up syntax is one thing, but structure is different. It's frusterating, I know, but recognizing mistakes yourself and a fuck-ton of trial and error gets your brain used to looking for things like that, so you don't make mistakes like that in the future. If you're serious about bein' a coder, just gotta start starin' at code and thinking a bit differently.. syntax and mystery errors are great for discussion, but basic 'how do I do this' problems, you should really try and write on paper and just draw arrows and diagrams and stuff.. that's one skill you REALLY need to learn yourself ![]() |
| | |
| | #56 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 2,173
| Lab pt. 1 import java.util.Scanner; public class Lab3Part1 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String stringOne; System.out.print("Please enter a string: "); stringOne = keyboard.next(); int len = stringOne.length(); if(len == 0) { System.out.println("Empty string."); } else if(Character.isLetter(stringOne.charAt(0)) && Character.isLetter(stringOne.charAt(len-1))) { System.out.println("Your string is a word."); } else if (Character.isDigit(stringOne.charAt(0)) && Character.isDigit(stringOne.charAt(len-1))) { System.out.println("Your string is a digit."); } else { System.out.println("Your string is unknown."); } } } Pt. 2 import java.util.Scanner; public class Lab3part3 { public static void main(String[] args) { int numberOne = 0, numberTwo = 0, finalScore = 0; Scanner keyboard = new Scanner(System.in); System.out.print("Please enter the first integer: "); numberOne = keyboard.nextInt(); System.out.print("Please enter the second integer: "); numberTwo = keyboard.nextInt(); System.out.print("What is the answer to " + numberOne + "*" + numberTwo + "?"); if ((numberOne * numberTwo) == keyboard.nextInt()) { System.out.println("You answered correctly!"); finalScore++; } else { System.out.println("Incorrect!"); } System.out.print("What is the answer to " + numberOne + "/" + numberTwo + "?"); if ((numberOne / numberTwo) == keyboard.nextInt()) { System.out.println("You answered correctly!"); finalScore++; } else { System.out.println("Incorrect!"); } System.out.print("What is the answer to " + numberOne + "%" + numberTwo + "?"); if ((numberOne % numberTwo) == keyboard.nextInt()) { System.out.print("You answered correctly!"); finalScore++; } else { System.out.println("Incorrect!"); } double avg = 0.0; avg = (float) finalScore / 4.0; switch(finalScore) { case 0: System.out.print("You did awful! \n You got zero right! \n Thats "); System.out.println(avg + "%"); break; case 1: System.out.print("You did...better. \n You got one right. \n Thats "); System.out.println(avg + "%"); break; case 2: System.out.print("You did OK. \n You got two right! \n Thats "); System.out.println(avg + "%"); break; case 3: System.out.print("You rock! \n You got all of them right! \n Thats "); System.out.println(avg + "%"); break; } }
__________________ Live like you'll die tomorrow, Dream like you'll live forever |
| | |
| | #58 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 2,173
| I'm stuck on this next part. I don't think I fully understand how "while" works. Our teacher told us it will work for the entire program and I've done the example in the lecture note, but it's still note working. System.out.print("I'm thinking of a number between 1 and 100, can you guess it?"); finalAnswer = keyboard.nextLine(); System.out.print("What's your first guess?"); guessAnswer = keyboard.nextInt(); rightAnswer = (int)(100 * Math.random ()) + 1; while(guessAnswer < rightAnswer){ System.out.print("Too low...try again!"); } if(guessAnswer > rightAnswer){ System.out.println("Too high...try again!");} guessAnswer = keyboard.nextInt(); if(guessAnswer == rightAnswer){ System.out.println("You got it right!"); guessAnswer = keyboard.nextInt(); } } } I'm basically trying to get the program to generate a random number and the user has to guess it correctly. I'm using while to loop the program if the user continuously fails to guess the answer correctly. The program itself works, but there are spacing issues (which I've tried to figure out, but it's not working) and the loop itself won't work. Can anyone hint to me what's going on? You don't have to give me the answer in code. Just a hint would be nice (like where to look at). edit: I looked at the nextInt(); and figured I didn't need to keep putting them, but when I remove those lines, it just stops working altogether. What the shit? lol
__________________ Live like you'll die tomorrow, Dream like you'll live forever Last edited by Kuriin : 10-11-2007 at 06:39 PM. |
| | |
| | #59 (permalink) |
| Fuckin' 07er Join Date: May 2007 Location: Philly
Posts: 340
| How while works: //Point A while( x < 10 ){ //code //Point B } //Point C If x is less than 10 upon Point A, it'll run the code once. If X is still less than 10 at Point B, it reruns the loop. Upon entering the while loop, you will not escape it unless X becomes equal or greater than 10, pretty much saying that the parameter of the while loop is WRONG. If you reach Point C, and X is less than 10, you do NOT go back to the while loop, if that's what you meant by 'last the entire program'. The while loop can only be entered when first encountered if it's parameter is true. Once inside the loop, it can only be exited by having the parameter become false. Example again: randomString = "blahblah"; while(randomString != "EXIT"){ output("To escape loop, type EXIT"); input(randomString); } So since randomString is NOT "EXIT", the loop gets entered. If you type something other than "EXIT", the loop repeats. If you type "EXIT", then randomString WOULD equal "EXIT", invalidating that parameter and breaking the loop. |
| | |
| | #60 (permalink) |
| Registered User Join Date: Jul 2002
Posts: 455
| while(guessAnswer < rightAnswer){ change that to while(guessAnswer != right Answer) that's the only thing you messed up on, then you just need to print that the answer is correct below the while loop (since the answer will equal the guess and end the loop) |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |