From 261a4386f054bb31da828540ba689cffb5956399 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Tue, 4 Apr 2023 17:49:16 +0200 Subject: [PATCH] Syntax error and postphoning exercise --- Doc/Sd1/Statements/statements.xml | 112 +++++++++++++++--------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/Doc/Sd1/Statements/statements.xml b/Doc/Sd1/Statements/statements.xml index 7ad1304cb..711d44201 100644 --- a/Doc/Sd1/Statements/statements.xml +++ b/Doc/Sd1/Statements/statements.xml @@ -673,60 +673,6 @@ int main(int argc, char **args) { </qandadiv> </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_replaceElseIf"> - <title>Replacing <code language="java">else if (...){...}</code> by - nested <code language="java">if ... else</code> statements</title> - - <qandadiv> - <qandaentry> - <question> - <para>A computer newbie did not yet read about the <code - language="java">else if(...)</code> branch construct but - nevertheless tries to implement the following logic:</para> - - <programlisting language="java">if (a < 7) { - System.out.println("o.K."); -} else if (5 == b) { - System.out.println("Maybe"); -} else { - System.out.println("Wrong!"); -}</programlisting> - - <para><code language="java">a</code> and <code - language="java">b</code> are supposed to be <code - language="java">int</code> variables. Please help our newbie - using just <code language="java">if(...){...} else {...} </code> - avoiding <code language="java"><emphasis role="red">else - if</emphasis>(...) {}</code> branch statements!</para> - - <tip> - <para>As the title suggests you may want to nest an - <quote>inner</quote> <code language="java">if(...)</code> - inside an <quote>outer</quote> one.</para> - </tip> - </question> - - <answer> - <para>The solution requires replacing the <code - language="java">else if(...)</code> branch by a nested <code - language="java">if(...){ ...} else {...}</code> statement by - moving the final <code language="java">else</code> block into - the nested one.</para> - - <programlisting language="java">if (a < 7) { - System.out.println("o.K."); -} else { - if (5 == b) { - System.out.println("Maybe"); - } else { - System.out.println("Wrong!"); - } -}</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - <figure xml:id="sd1_fig_IfOmitBlocks"> <title>Single statement branches</title> @@ -841,6 +787,60 @@ else </calloutlist> </figure> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_replaceElseIf"> + <title>Replacing <code language="java">else if (...){...}</code> by + nested <code language="java">if ... else</code> statements</title> + + <qandadiv> + <qandaentry> + <question> + <para>A computer newbie did not yet read about the <code + language="java">else if(...)</code> branch construct but + nevertheless tries to implement the following logic:</para> + + <programlisting language="java">if (a < 7) { + System.out.println("o.K."); +} else if (5 == b) { + System.out.println("Maybe"); +} else { + System.out.println("Wrong!"); +}</programlisting> + + <para><code language="java">a</code> and <code + language="java">b</code> are supposed to be <code + language="java">int</code> variables. Please help our newbie + using just <code language="java">if(...){...} else {...} </code> + avoiding <code language="java"><emphasis role="red">else + if</emphasis>(...) {}</code> branch statements!</para> + + <tip> + <para>As the title suggests you may want to nest an + <quote>inner</quote> <code language="java">if(...)</code> + inside an <quote>outer</quote> one.</para> + </tip> + </question> + + <answer> + <para>The solution requires replacing the <code + language="java">else if(...)</code> branch by a nested <code + language="java">if(...){ ...} else {...}</code> statement by + moving the final <code language="java">else</code> block into + the nested one.</para> + + <programlisting language="java">if (a < 7) { + System.out.println("o.K."); +} else { + if (5 == b) { + System.out.println("Maybe"); + } else { + System.out.println("Wrong!"); + } +}</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + <figure xml:id="sd1_fig_useScannerClass"> <title>User input recipe</title> @@ -856,7 +856,7 @@ public class App { public static void main(String[] args){ final Scanner scan = - new Scanner(System.in)); + new Scanner(System.in); System.out.print("Enter a value:"); final int value = scan.nextInt(); System.out.println("You entered " @@ -1236,7 +1236,7 @@ Decimal value 11 not yet implemented</screen> </question> <answer> - <para><programlisting language="java">final Scanner scan = new Scanner(System.in)); + <para><programlisting language="java">final Scanner scan = new Scanner(System.in); System.out.print("Enter a number:>"); final int number = scan.nextInt(); -- GitLab