• Register
2 votes
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 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,240 points
0 votes
Thanks for answer was really helpful
0 points

Related questions

0 votes
1 answer 416 views
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; ... 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 17 views
17 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.2k points
0 votes
1 answer 9 views
9 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 22 views
22 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 5.6k points
0 votes
1 answer 205 views
205 views
Problem : I am very new to r programming. I have the array and I want to implement the below method: x<-function(b){ if (b>0){ b/sum(b) } else 1 } This method is written to check if there is any value which is larger than the 0 and if it says ... message: &ldquo;Warning message: r the condition has length > 1 and only the first element will be used&rdquo;. How can I fix the above error message?
asked Feb 24, 2020 mphil 2.3k points
0 votes
1 answer 15 views
15 views
Problem: Hello all, I just recently started learning Java. In today&rsquo;s class, we learned about ternary operator and my teacher gave me an assignment in which I have to convert given if-else statement using ternary operator: if(number1<number2 && number1 < number3 ... out the smallest number"); I am unable to figure out how to change these statements using ternary operators. Can anybody help?
asked Nov 30, 2020 Code Learner 5.2k points
0 votes
1 answer 37 views
37 views
Problem: I am very new to the Java. I am currently trying to understand the if statements in java. I have one doubt as follows if(a && b || c || d || e) then whether it will check (a && b) if a and b must be always true then only will it allow inside ... a && e))here if any of the conditions are true then will it allow inside ? Please guide me in solving my doubts related to if statements in java.
asked Jul 29, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 29 views
29 views
Problem: Let me put it as sim as I can. I have an if statement in my code and it returns a value. Since IF is a boolean statement it should return value whether it&rsquo;s true or false. So, in my program when a method tests an argument and returns a true or ... as a parameter in a different method inside an IF statement. How should I do that? Any kind of help is appreciated by my side. Thanks.
asked Mar 17, 2020 Gavin 15.3k points
0 votes
1 answer 11 views
11 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 20 views
20 views
I am finding difficulty to convert categotical data to numerical data in Python.
asked Sep 29, 2020 Daniel Anderson 4k points