Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

How can i write a while loop that checks to validate the first number is even and less than the second number. And if it isnt an error message is printed and the user is asked if two more integers are desired?

OpenStudy (anonymous):

my code so far is: import java.util.Scanner; public class Calculate { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("\tSum & Multiplication Calculator\n"); int num1, num2; System.out.print("Enter two integers: "); num1 = keyboard.nextInt(); num2 = keyboard.nextInt(); String input; System.out.println("Enter a string: "); input = keyboard.next(); if(input.equals("add")) { int sum; sum = add(num1, num2); System.out.print("The sum of two even numbers is: " + sum); } else if(input.equals("multiply")) { int product; product = multiply(num1, num2); System.out.print("The product of two integers is: " + product); } else { System.out.println("Error, invalid input!"); } } public static int add(int number1, int number2) { int sum = 0, index = 0; for(index = number1; index <= number2; index++) { if(index%2==0) { sum+=index; } } return sum; } public static int multiply(int number1, int number2) { int result; result = number1 * number2; return result; } }

OpenStudy (anonymous):

@thomaster how would i be able to do write one/

OpenStudy (anonymous):

@e.mccormick how would i be able to write one?

OpenStudy (e.mccormick):

When people copy code from here, it can collapse all onto one line, making it hard to work with. For short code, putting ``` above and below the block stops this. For longer code blocks, code pasting sites are great: http://dpaste.com http://pastebin.com https://gist.github.com/ http://pastie.org http://codepad.org http://ideone.com http://www.repl.it/

OpenStudy (anonymous):

ok i'll do that first then...be back in a min

OpenStudy (anonymous):

http://dpaste.com/1393103/ this is my code

OpenStudy (e.mccormick):

Kk. Thanks. Makes it easier to try things. I like dpaste for quick stuff I intend to have go away soon. Pastebin is very nice for stuff that lasts longer. And many people love others.

OpenStudy (anonymous):

i didn't know these kind of websites existed

OpenStudy (e.mccormick):

Neither did I till I came here! A while or do while loop is geneally best for mandetory input.

OpenStudy (anonymous):

the whole code besides the static methods will be in a do-while loop..the do-while loop is supposed to repeat the whole program if a yes or Yes is entered the while loop is supposed to be used for input validation if "add" is indicated....i've tried but the while loop i was attempting had syntax errors or infinite loops

OpenStudy (e.mccormick):

``` boolean verify = true; while(verify){ get some input if correct verify = false else error mesage/re-prompt } ```

OpenStudy (anonymous):

ok and verify is the boolean expression itself

OpenStudy (e.mccormick):

Well, it is a variable. You are just using true and false because those are how loops are exited. While it is true, it loops. So you set it to false to exit the loop cleanly.

OpenStudy (e.mccormick):

See, it is best to clean up input before it is passed to the other function. Or you need to use excepton handling in that function.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!