CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
I.A
Level 7
unknown city
  • 19.11.2018
  • 1162views
  • 9comments

why not its ok

Question about the task Positive and negative numbers
Java Syntax,  Level 4,  Lesson 4
Archived


Use the keyboard to enter a number.
If the number is positive, then double it.
If the number is negative, add one.
If the entered number is zero, display 0.
Display the result on the screen.

Requirements:
  • The program should read a number from the keyboard.
  • The program must display a number on the screen.
  • If the entered number is positive, double it and display the result.
  • If the entered number is negative, increase it by one and display the result.
  • If the entered number is zero, display 0.
package com.codegym.task.task04.task0412; /* Positive and negative numbers */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //write your code here InputStream input = System.in; InputStreamReader reader= new InputStreamReader(input); BufferedReader br=new BufferedReader(reader); // System.out.println("please enter a num"); String num = br.readLine(); int number= Integer.parseInt(num); if(number>0) { number=number*2; System.out.println(number);} if (number<0){ // number=number*(-1); number= (number/-1) + 1; System.out.println(number);} if (number==0) System.out.println(number); } }
0
Comments (9)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
MaGaby2280
Level 41 , Guatemala City, Guatemala
16 September 2019, 17:44
This is what I did: import java.util.Scanner; public class Solution { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); if (x > 0) { x = x * 2; System.out.println(x); } else if (x < 0){ x = x + 1; System.out.println(x); } else if (x == 0){ x = 0; System.out.println(x); } } }
0
divya
Level 5 , hyderabad, India
23 November 2018, 07:47
if (num<0) num= num + 1; if the number is less than "0" it should be increased by one.no need of convertion
0
Khurram
Level 16 , Lahore, Pakistan
20 November 2018, 05:33
you should add 1 to a negative number, example (-5+1 = -4), here you are converting it to a positive number and then adding 1
+1
I.A
Level 7 , unknown city, Oman
26 November 2018, 14:10
ans is not 6 its -4 so how could say ans is increasing by 1
0
Michael Martin
Level 19 , Arlington, United States
26 November 2018, 14:39
Because adding 1 to -5 = -4. (-5 + 1) = -4
0
I.A
Level 7 , unknown city, Oman
26 November 2018, 14:43
ok igot but same error with separate if statements technique?what is reason behind it
0
Michael Martin
Level 19 , Arlington, United States
26 November 2018, 15:28
can you post your new code?
0
I.A
Level 7 , unknown city, Oman
26 November 2018, 16:09
package com.codegym.task.task04.task0412; /* Positive and negative numbers */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //write your code here InputStream input = System.in; InputStreamReader reader= new InputStreamReader(input); BufferedReader br=new BufferedReader(reader); // System.out.println("please enter a num"); String num = br.readLine(); int number= Integer.parseInt(num); if(number>0) { number=number*2; System.out.println(number);} else if (number<0){ // number=number*(-1); number= number + 1; System.out.println(number); } //(number==0) else System.out.println("0"); } }
0
Michael Martin
Level 19 , Arlington, United States
26 November 2018, 16:20
Not sure why it's not working. Try:
if (number == 0)
   System.out.println(number);
if (number < 0)
   System.out.println(number + 1);
if (number > 0)
   System.out.println(number * 2);
+2
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.