Example
#{example}"); ipb.editor_values.get('templates')['togglesource'] = new Template(""); ipb.editor_values.get('templates')['toolbar'] = new Template(""); ipb.editor_values.get('templates')['button'] = new Template("
Emoticons
"); // Add smilies into the mix ipb.editor_values.set( 'show_emoticon_link', false ); ipb.editor_values.set( 'bbcodes', $H({"snapback":{"id":"1","title":"Post Snap Back","desc":"This tag displays a little linked image which links back to a post - used when quoting posts from the board. Opens in same window by default.","tag":"snapback","useoption":"0","example":"[snapback]100[/snapback]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"topic":{"id":"5","title":"Topic Link","desc":"This tag provides an easy way to link to a topic","tag":"topic","useoption":"1","example":"[topic=1]Click me![/topic]","switch_option":"0","menu_option_text":"Enter the topic ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"post":{"id":"6","title":"Post Link","desc":"This tag provides an easy way to link to a post.","tag":"post","useoption":"1","example":"[post=1]Click me![/post]","switch_option":"0","menu_option_text":"Enter the Post ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"spoiler":{"id":"7","title":"Spoiler","desc":"Spoiler tag","tag":"spoiler","useoption":"0","example":"[spoiler]Some hidden text[/spoiler]","switch_option":"0","menu_option_text":"","menu_content_text":"Enter the text to be masked","single_tag":"0","optional_option":"0","image":""},"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]","switch_option":"0","menu_option_text":"Enter the description for this acronym (EG: Laugh Out Loud)","menu_content_text":"Enter the acronym (EG: lol)","single_tag":"0","optional_option":"0","image":""},"hr":{"id":"12","title":"Horizontal Rule","desc":"Adds a horizontal rule to separate text","tag":"hr","useoption":"0","example":"[hr]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"1","optional_option":"0","image":""},"php":{"id":"14","title":"PHP Code","desc":"Allows you to enter PHP code into a formatted/highlighted syntax box","tag":"php","useoption":"0","example":"[php]$variable = true;\n\nprint_r($variable);[/php]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"html":{"id":"15","title":"HTML Code","desc":"Allows you to enter formatted/syntax-highlighted HTML code","tag":"html","useoption":"0","example":"[html]\n \n[/html]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"sql":{"id":"16","title":"SQL Code","desc":"Allows you to enter formatted/syntax-highlighted SQL code","tag":"sql","useoption":"0","example":"[sql]SELECT p.*, t.* FROM posts p LEFT JOIN topics t ON t.tid=p.topic_id WHERE t.tid=7[/sql]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"xml":{"id":"17","title":"XML Code","desc":"Allows you to enter formatted/syntax-highlighted XML code","tag":"xml","useoption":"0","example":"[xml]29 Replies - 185 Views - Last Post: Yesterday, 09:58 PM
#1
Reputation: 0
- Posts: 36
- Joined: 10-March 13
Posted Yesterday, 07:05 PM
//I am getting an error on line 29, stating "else without if" //this program will ask you enter a number, it its too high or low, re-enter it import java.util.Scanner; class doWhile2{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int i = (int)(Math.random() * 100); int number; // create do while do{ System.out.println("Enter an integer: "); number = input.nextInt(); } while(true); if (i == number){ System.out.println("The number you entered matches our number " + i); } break; else if (i > number){ System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: "); } else { System.out.println("The number you entered is too high, the differnce is " (number - i)" try again: "); } } }
Is This A Good Question/Topic? 0
Replies To: If else statement wont work for do while loop
#2
Reputation: 20
- Posts: 60
- Joined: 22-April 13
Re: If else statement wont work for do while loop
Posted Yesterday, 07:07 PM
Remove the break or put it inside the if statement.This post has been edited by schutzzz: Yesterday, 07:08 PM
#3
Reputation: 5524
- Posts: 8,925
- Joined: 19-March 11
Re: If else statement wont work for do while loop
Posted Yesterday, 07:08 PM
The else must follow the true branch directly. Here you have an intervening break instruction, which means the if statement has no else block. If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.
#4
Reputation: 0
- Posts: 36
- Joined: 10-March 13
Re: If else statement wont work for do while loop
Posted Yesterday, 07:35 PM
jon.kiparsky, on 09 June 2013 - 07:08 PM, said:
The else must follow the true branch directly. Here you have an intervening break instruction, which means the if statement has no else block. If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.
The reason why I put the break is so that if the user input a matching number, then the program should end. Am I thinking correctly? Also, I just removed the break, but now I am getting an error stating: "unexpected type" for the minus symbol in both my ELSE IF and ELSE statement.
#5
Reputation: 5524
- Posts: 8,925
- Joined: 19-March 11
Re: If else statement wont work for do while loop
Posted Yesterday, 07:37 PM
jon.kiparsky, on 09 June 2013 - 09:08 PM, said:
If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.
#6
Reputation: 0
- Posts: 36
- Joined: 10-March 13
Re: If else statement wont work for do while loop
Posted Yesterday, 07:39 PM
schutzzz, on 09 June 2013 - 07:07 PM, said:
Remove the break or put it inside the if statement.
But isnt the break so if my IF statement is true, then it will ignore the following statements? I've taken off the break and I added a + to my ELSE statement, but now I am getting the error: "unexpected type" for both my ELSE IF and ELSE statement, which is pointing to the subtraction sign...so now I have the following code:
while(true); if (i == number){ System.out.println("The number you entered matches our number " + i); } else if (i > number){ System.out.println("The number you entered is too low, the difference is " + (i - number) " try again: "); } else { System.out.println("The number you entered is too high, the differnce is " + (number - i) " try again: "); }
#7
Reputation: 5524
- Posts: 8,925
- Joined: 19-March 11
Re: If else statement wont work for do while loop
Posted Yesterday, 07:40 PM
streek405, on 09 June 2013 - 09:35 PM, said:
System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: ");
You're missing a plus sign.
System.out.println("The number you entered is too low, the difference is " + (i - number) + " try again: ");
#8
Reputation: 0
- Posts: 36
- Joined: 10-March 13
Re: If else statement wont work for do while loop
Posted Yesterday, 07:45 PM
jon.kiparsky, on 09 June 2013 - 07:37 PM, said:
jon.kiparsky, on 09 June 2013 - 09:08 PM, said:
If the break is meant to be part of the true branch (if you want to break in the cases where i is equal to number) then it must go inside the curly braces.
Oh ok. I think I understand it now. But when I put the break inside the curly braces, I get an error stating "break outside switch or loop". So now I just omitted the break entirely.
#9
Reputation: 5524
- Posts: 8,925
- Joined: 19-March 11
Re: If else statement wont work for do while loop
Posted Yesterday, 07:46 PM
streek405, on 09 June 2013 - 09:39 PM, said:
But isnt the break so if my IF statement is true, then it will ignore the following statements?
The syntax of the if statement is like this:
if (CONDITION)
STATEMENT BLOCK
else STATEMENT BLOCK
a STATEMENT BLOCK is eitehr a single statement:
if (true) System.out.println("It's twue! It's twue!");
which can be an empty statement:
if (true) ;
or some number of statements in curly braces:
if (true) { System.out.println("It's twue! It's twue!"); truthIsTrue = true; int one = 1; // etc. }
The STATEMENT BLOCK terminates after the closing curly brace (if you use braces) or after the semicolon terminating the single statement.
Your break statement is not part of the if (and so, as written, it will always execute). Therefore, your else statements have no if to else themselves from, so the compiler complains.
#10
Reputation: 0
- Posts: 36
- Joined: 10-March 13
Re: If else statement wont work for do while loop
Posted Yesterday, 07:49 PM
jon.kiparsky, on 09 June 2013 - 07:40 PM, said:
streek405, on 09 June 2013 - 09:35 PM, said:
System.out.println("The number you entered is too low, the difference is " + (i - number)" try again: ");
You're missing a plus sign.
System.out.println("The number you entered is too low, the difference is " + (i - number) + " try again: ");
Thank you, I too noticed that, but now I am getting the error "unexpected type" for both the ELSE IF and ELSE statement, pointing at the minus sign
jon.kiparsky, on 09 June 2013 - 07:46 PM, said:
streek405, on 09 June 2013 - 09:39 PM, said:
But isnt the break so if my IF statement is true, then it will ignore the following statements?
The syntax of the if statement is like this:
if (CONDITION)
STATEMENT BLOCK
else STATEMENT BLOCK
a STATEMENT BLOCK is eitehr a single statement:
if (true) System.out.println("It's twue! It's twue!");
which can be an empty statement:
if (true) ;
or some number of statements in curly braces:
if (true) { System.out.println("It's twue! It's twue!"); truthIsTrue = true; int one = 1; // etc. }
The STATEMENT BLOCK terminates after the closing curly brace (if you use braces) or after the semicolon terminating the single statement.
Your break statement is not part of the if (and so, as written, it will always execute). Therefore, your else statements have no if to else themselves from, so the compiler complains.
Ok thank you, that actually makes sense!
#11
Reputation: 12
- Posts: 64
- Joined: 12-May 09
Re: If else statement wont work for do while loop
Posted Yesterday, 07:50 PM
you start your while statement and immediately end it when you do thiswhile(true);
it should be
while(true){
#12
Reputation: 0
- Posts: 36
- Joined: 10-March 13
Re: If else statement wont work for do while loop
Posted Yesterday, 07:52 PM
Bountyhunter1234, on 09 June 2013 - 07:50 PM, said:
you start your while statement and immediately end it when you do thiswhile(true);
it should be
while(true){
I originally made it like that, but then I got the error " ; expected", point that the curly brace
#13
Reputation: 5524
- Posts: 8,925
- Joined: 19-March 11
Re: If else statement wont work for do while loop
Posted Yesterday, 07:54 PM
If you fix that missing plus sign, that will go away.Also, there's no reason why your break should be giving you an error if you put it inside the curly braces.
See the following sample code. Try compiling it in a throwaway file:
public class Foo { public static void main(String[] args) { int i = 0; int number = 10; System.out.println("The number you entered is too low, the difference is" + (i - number) +" try again: "); while (true) { if (true) { System.out.println ("true"); break;} } } }
This post has been edited by jon.kiparsky: Yesterday, 07:59 PM
#14
Reputation: 12
- Posts: 64
- Joined: 12-May 09
Re: If else statement wont work for do while loop
Posted Yesterday, 07:57 PM
my fault i didn't realize it was a do-while, so forget my post i was looking at it as a while statement.
#15
Reputation: 0
- Posts: 36
- Joined: 10-March 13
Re: If else statement wont work for do while loop
Posted Yesterday, 08:03 PM
jon.kiparsky, on 09 June 2013 - 07:54 PM, said:
If you fix that missing plus sign, that will go away.Also, there's no reason why your break should be giving you an error if you put it inside the curly braces.
See the following sample code. Try compiling it in a throwaway file:
public class Foo { public static void main(String[] args) { int i = 0; int number = 10; System.out.println("The number you entered is too low, the difference is" + (i - number) +" try again: "); while (true) { if (true) { System.out.println ("true"); break;} } } }
Bountyhunter1234, on 09 June 2013 - 09:50 PM, said:
you start your while statement and immediately end it when you do thiswhile(true);
it should be
while(true){
I've tried that but I am still getting the same error, for the break. This the the code
while(true); if (true){ System.out.println("The number you entered matches our number " + i); break; } else if (i > number){ System.out.println("The number you entered is too low, the difference is " + (i - number) " try again: "); } else { System.out.println("The number you entered is too high, the differnce is " + (number - i) " try again: "); }
Good catch! That is in fact the problem.
LOL That was my very first attempt that this code, but it stated that I need an ";" in place of the curly brace for the WHILE. Also, in my book, for the do while example, there is an ";" not a curly brace for the while part, although that does not make any sense to me...
Bountyhunter1234, on 09 June 2013 - 07:57 PM, said:
my fault i didn't realize it was a do-while, so forget my post i was looking at it as a while statement.
It's all good, man.
Source: http://www.dreamincode.net/forums/topic/322728-if-else-statement-wont-work-for-do-while-loop/
After Christmas Sales 2012 Charles Durning Webster Ny Mcdonalds Restaurants Open on Christmas Day jessica simpson santa tracker
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.