• Register
2 votes
231 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 positive. So, I am struggling with a problem like converting a negative number to 0 or 1 in my program. I’ve got a shallow concept in my mind 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.

 

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

Hello Gavin,

You’ve asked an interesting but very basic question. No shame! I appreciate your asking. You can solve this problem by using an if-statement in your program. If-else will check your number and then return it as you wanted. Let’s start:

public class NegToPos {
    public static void main (String [] args) {
        int Num = 0;
        if (Num >= 0)
            System.out.println("It's a non negative number");
        else{
            System.out.println("It's a negative nummber; now converting to 1");
            Num = 1;
            System.out.format("Final: %d", Num);
            System.out.println(""); 
        }
    }
}

So, this code should convert non-positive numbers to 1. Thanks again. Keep asking.

13 9 6
94,260 points
0 votes
Thanks for answer was really helpful
0 points

Related questions

0 votes
1 answer 574 views
574 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; ... out.format("Final Result: %d", myUserNum); System.out.println(""); return; } } Please let me know how can I fix this issue?
asked Dec 3, 2019 alecxe 7.5k points
0 votes
1 answer 62 views
62 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 9.8k points
0 votes
1 answer 6 views
6 views
Problem: how do I check the return type of the boolean method within the parameters of an if statement is? Your answers are deeply appreciated.
asked Apr 25 ummeshani 8.8k points
0 votes
1 answer 27 views
27 views
I do not know the conversion process from one type to another type. So I want to convert an integer into a binary value. What is the code involved in it. Does it have any in built function to perform this operation?
asked Dec 21, 2020 ZarinaShaik 320 points
0 votes
1 answer 44 views
44 views
Problem: How to convert decimal to binary, Java has a method &ldquo;Integer. toBinaryString()&rdquo;. The method returns a string representation of the integer argument as an unsigned integer in base 2.N
asked Nov 5, 2020 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
Problem: How can I simply convert the whole number to decimal? Is there a JS method I can use to do this?
asked Apr 22 muktaa 34.4k points
0 votes
1 answer 2 views
2 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 24 rubelkhan 20.4k points
0 votes
1 answer 46 views
46 views
Problem: Hello Kodlogs, i am trying to convert decimal number to binary number .Is there any built in module/function in python so that i can use that for my conversion.Can any one help me with this? Thanks in advance
asked Oct 8, 2020 kalyani456 280 points
0 votes
1 answer 38 views
38 views
I am finding difficulty to convert categotical data to numerical data in Python.
asked Sep 29, 2020 Daniel Anderson 4k points
0 votes
1 answer 13 views
13 views
How do you convert a numerical number to an Excel column name in C# without using automation getting the value directly from Excel.?
asked Aug 28, 2020 Aliza313 720 points