Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Scope of num variable is in main method only.either pass it as an argument or make it global. And please refer to java tutorials.</p> <blockquote> <p><strong>WITH NUM AS ARGUEMENT</strong></p> </blockquote> <pre><code>package com; import java.util.Scanner; public class WorkSheet_1 { //static int[] num = new int[3]; public static void main(String[] args) { //call for input System.out.println("Please Enter a 3-digit number.."); Scanner in = new Scanner(System.in); int[] num = new int[3]; for(int i = 0; i &lt; num.length; i++){ int val = in.nextInt(); num[i] = val; } System.out.println("The Sum of the numbers is " + sumNums(num)); System.out.println("The Reverse of the numbers is " + reverseNums(num)); } public static int sumNums(int[] num) { return num[0] + num[1] + num[2]; } public static int reverseNums(int[] num) { return num[2] + num[1] + num[0]; } } </code></pre> <blockquote> <p><strong>WITH NUM AS GLOBAL(STATIC)</strong></p> </blockquote> <pre><code>package com; import java.util.Scanner; public class WorkSheet_1 { static int[] num = new int[3]; public static void main(String[] args) { //call for input System.out.println("Please Enter a 3-digit number.."); Scanner in = new Scanner(System.in); for(int i = 0; i &lt; num.length; i++){ int val = in.nextInt(); num[i] = val; } System.out.println("The Sum of the numbers is " + sumNums()); System.out.println("The Reverse of the numbers is " + reverseNums()); } public static int sumNums() { return num[0] + num[1] + num[2]; } public static int reverseNums() { return num[2] + num[1] + num[0]; } } </code></pre>
    singulars
    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