Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your program has a Java syntax error, for which the compiler complains, and a more subtle logical error which is pretty common even among experienced programmers across the entire C/C++/C#/Java family.</p> <p>The syntax error is that the kind of <strong>for</strong> that you used needs the you specify a collection after the colon; or, more correctly, an object of a type which implements interface <strong>Iterable</strong>. Interfaces define a <em>contract</em> that authorizes the compiler to perform certain operations on the instances of a class which implement the interface. So, the compiler will handle iteration over a variable only if the variable is <strong>Iterable</strong>. Since in your case <strong>rolls</strong> is of type <strong>int</strong> (and hence is not even a class) and <strong>int</strong> is not <strong>Iterable</strong>, you get the error.</p> <p>You <em>iterate</em> on integers using the <em>classic</em> <strong>for</strong>, which is supported pretty much with no differences in C, C++, C# and Java.</p> <pre><code>for (int num1 = 0; num1 &lt; rolls; num1 = num1 + 1) /* A single statement (body) */; </code></pre> <p>This statements means: repeat the <em>body</em>, that is the <em>single</em> statement mentioned after the <strong>for (...)</strong>, exactly <strong>rolls</strong> times. More correctly, it implements the following algorithm:</p> <pre><code>A. Initialize num1 to 0 B. Is num1 lesser than rolls? If yes, execute the body, increment num1 and go back to B If not, go to the first instruction after the **for** </code></pre> <p>Now, the second problem; this could happen with both flavors of the <strong>for</strong>, but it is unlikely to go unnoticed with the <strong>Iterable</strong> flavor. You most likely don't want a semicolon right after the <strong>for</strong> statement; here's why. A design decision for C and all languages with C-like syntax, like Java, is that in most places where you want to insert one or more statements, only one statement can actually be inserted. The <strong>for</strong> cycle can handle only a single instruction in its body, and it's going to be the one after the <em>)</em>, disregarding indentation and coding style. Consider the following two <strong>for</strong> cycles:</p> <pre><code>// Prints numbers from 0 to 9 int x = 0; for (x = 0; x &lt; 10; x = x + 1) System.out.println(x); // Prints 10 int y = 0; for (y = 0; y &lt; 10; y = y + 1); System.out.println(y); </code></pre> <p>They look alike, but the effect of the first code fragment is printing the numbers from 0 to 9, while the effect of the second is printing 10. The reason is that the body of the second for is actually an empty statement. You could rewrite the two *<em>for</em>*s like this:</p> <pre><code>int x = 0; for (x = 0; x &lt; 10; x = x + 1) System.out.println(x); int y = 0; for (y = 0; y &lt; 10; y = y + 1) /* Lalala... does nothing at all... */; System.out.println(y); // When control flow gets here, y equals 10 </code></pre> <p>To put more than a statement inside a <strong>for</strong>, you must use a <strong>block</strong>, which is a statement composed of many statements. What's misleading for the beginner, is that blocks doesn't have to be associated with <strong>for</strong>, and actually can exist on their own. The following code in Java is legal:</p> <pre><code> { num = generator.nextInt(sides); System.out.println("You rolled a " + num); } </code></pre> <p>When you put a <strong>for</strong> which erroneously include the semicolon at the end:</p> <pre><code> for (int num1 = 0; num1 &lt; rolls; num1 = num1 + 1); { num = generator.nextInt(sides); System.out.println("You rolled a " + num); } </code></pre> <p>you are basically saying:</p> <pre><code> for (int num1 = 0; num1 &lt; rolls; num1 = num1 + 1) /* Lalala... does nothing at all... */; { num = generator.nextInt(sides); System.out.println("You rolled a " + num); } </code></pre> <p>Hence your for is executing <em>nothing</em> exactly <strong>rolls</strong> times, and then it executes the block statement once. Sometimes such errors are mitigated by the fact that the body of a <strong>for</strong> would reference the iteration variable, <strong>num1</strong>, but it is not your case. Consider:</p> <pre><code> for (int num1 = 0; num1 &lt; rolls; num1 = num1 + 1); { Console.out.println("Roll No. " + num1); num = generator.nextInt(sides); System.out.println("You rolled a " + num); } </code></pre> <p>This has the ; issue but the compiler detects that there is something wrong because you are using <strong>num1</strong> inside the block statement, but due to the ; the block statement was not associated with the <strong>for</strong>, and hence <strong>num1</strong> is out of scope. The following, although having the same logical error, would not result in a compiler error:</p> <pre><code> for (int num1 = 0; num1 &lt; rolls; num1 = num1 + 1); { num = generator.nextInt(sides); System.out.println("You rolled a " + num); } </code></pre> <p><strong>Note</strong>: also your <strong>while</strong> has a similar issue of the extra ; character.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload