• Register
0 votes
416 views

Problem :

I am facing issues with following Java  code and unable to figure out the cause behind the issue, I guess it is some logical error but I am unable to figure it out, my all results are ending up as 0.

Please find below the Java Code:

import java.util.Scanner;
public class MyConvertNegative {
   public static void main (String [] args) {
      int myUserNum = 0;
      if (myUserNum >= 0)
         System.out.println("Poitive");
      else
         System.out.println("Negative; Converting To 0");
         myUserNum = 0;
         System.out.format("Final Result: %d", myUserNum);
         System.out.println(""); 
      return;
   }
}

Please let me know how can I fix this issue?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

In your code I can see that you have the multi line conditions so you need to set the brackets. Without them your else block will just execute the first line of the code after it. So in your case the line executed is  System.out.println("Negative; Converting To 0");. So in that case your variable myUserNum would be set to 0 as the next line will not be the part of the else block anymore. So you need to follow the More than 1 line = brackets.

public class MyConvertNegative {
    public static void main (String [] args) {
        int myUserNum = 0;
        if (myUserNum >= 0)
            System.out.println("Poitive");
        else{
            System.out.println("Negative; Converting To 0");
            myUserNum = 0;
            System.out.format("Final Result: %d", myUserNum);
            System.out.println(""); 
        }
        //return; -> No need for the return as your main function is not returning any value.
    }
}
9 7 4
38,600 points

Related questions

2 votes
2 answers 187 views
187 views
Problem: Hello all, I am a noob to this java programming. I started learning it a few days back. Re-type the code and fix any errors. the code should convert non-positive numbers to 1. As long as I understand this problem is sying to convert a negative number to ... but that’s not enough for my case. If your understanding is up to this level, please pull me up from the grave LOL. Thanks.
asked Mar 19, 2020 Gavin 15.3k points
0 votes
1 answer 15 views
15 views
Problem: Hi there! I am a noob and I have written following program: public class Negative{ public static void main(String []args){ int number = 5; System.out.println("The number before check is: " + number); if(number < 0) number = 0; else number = 0; System.out ... to 0. I have no clue what I am doing wrong. If there are any errors in my program please correct and mention it in comment. Thanks
asked Nov 27, 2020 Code Learner 5.1k points
0 votes
1 answer 21 views
21 views
Please explain the following about "Cannot find symbol", "Cannot resolve symbol" or "Symbol not found" errors in JAVA?
asked Sep 28, 2020 Xaimax 9.5k points
0 votes
1 answer 8 views
8 views
Can anyone help me by solving this problem?
asked Dec 16, 2020 TeamScript 9.2k points
0 votes
1 answer 12 views
12 views
I want to know how to fix this. Could anyone here help me out?
asked Dec 16, 2020 TeamScript 9.2k points
0 votes
1 answer 9 views
9 views
I have a Java project I am working on and I get this error. I am new to this. I just want to know how to do that? help me .
asked Dec 3, 2020 TeamScript 9.2k points
1 vote
1 answer 310 views
310 views
Problem: I wrote a program in python that asks the user for a number n and prints the sum of the numbers 1 to n: x = 1, addition = 0 n = int(input('Please enter a number:')) for i in range(x, n): addition = addition + i print (addition) But when ... n): TypeError: 'tuple' object cannot be interpreted as an integer Is there any thing that is missing from my code. Please help me to solve this out.
asked Jun 10, 2020 samhaz 5k points
0 votes
1 answer 7 views
1 vote
1 answer 38 views
38 views
Problem: For the following two lists: 1stlist = [-5, 10, -8, 16, -20] 2ndlist = [-24, 33, -55, -90, 11] I wrote a pyhton program where I can Count positive and negative numbers in this two lists : 1stlist = [-5, 10, -8, 16, -20] 2ndlist = [-24, 33, ... py_compile.PyCompileError: File "./prog.py", line 1 1stlist = [-5, 10, -8, 16, -20] ^ SyntaxError: invalid syntax How can I solve this problem?
asked Jun 1, 2020 samhaz 5k points
0 votes
1 answer 320 views