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
Nitin
Level 5
  • 08.03.2019
  • 938views
  • 2comments

Need Help!

Question about the task Positive number
Java Syntax,  Level 4,  Lesson 7
Under discussion


Use the keyboard to enter three integers. Display the number of positive numbers in the original set.

Here are some examples:
a) if you enter the numbers
-4
6
6
then we display
2

b) if you enter the numbers
-6
-6
-3
then we display
0

c) if you enter the numbers
0
1
2
then we display
2

Requirements:
  • The program should read the numbers from the keyboard.
  • The program must display a number on the screen.
  • The program should display the number of positive numbers in the original set.
  • If there are no positive numbers, the program should display "0".
  • Note that 0 is not a positive or negative number.
package com.codegym.task.task04.task0428; /* Positive number */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String x = reader.readLine(); String y = reader.readLine(); String z = reader.readLine(); int a = Integer.parseInt(x); int b = Integer.parseInt(y); int c = Integer.parseInt(z); if((a<0&&b>0&&c>0) || (a>0&&b<0&&c>0) || (a>0&&b>0&&c<0)) { System.out.println("2"); } else if((a<0&&b<0&&c>0) || (a<0&&b>0&&c<0) || (a>0&&b<0&&c<0)) { System.out.println("1"); } else { System.out.println("0"); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Dorin Miron
Level 29 , Brent, England
8 March 2019, 13:54
you can use a static variable(static int x=0) and increase this variable (x++) every time you have a>0 or b>0 or c>0 ant then print this static variable.
+1
Ercian
Level 5 , Mashivka, Ukraine
8 March 2019, 11:32
Because your code considers 0 as a positive number. It should not be considered as a positive or a negative. P.S. What happens, if you enter all three positive numbers?
0
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.