0

Anyone can fix my code. Please help me😇,

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //x % 2 == 0 - Even number //((c/2)*2 == c) - Odd number System.out.print("Enter a number you want: "); int n = sc.nextInt(); System.out.println("Type a random numbers: "); int[] num = new int[n]; for (int x=1; x <= n; x++){ System.out.print("Number "+x+": "); int ran = sc.nextInt(); if (ran % 2 == 0){ int result = ran + 1; }else{ int result = ran - 1; } System.out.print("result: "+result);//the line to be fix } } }

15th Feb 2020, 2:29 PM
Vic Izumi Kun Oguan
Vic Izumi Kun Oguan - avatar
1 Resposta
+ 3
Define int result=0; outside the for loop.
15th Feb 2020, 3:07 PM
Avinesh
Avinesh - avatar