CodeGym
促销活动
CodeGym University
学习
课程
任务
调查和小测验
游戏
帮助
提醒时间表
社区
用户
论坛
聊天
文章
成功故事
活动
评论
订阅服务
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 全部问题
建飞 蒲
第 6 级
Chengdu
  • 20.05.2020
  • 673次阅读
  • 5个评论

为啥就是错的,这是bug吗

有关 任务的问题 正数和负数
Java 语法,  第 4 级,  课程 7
正在讨论


使用键盘输入三个整数。显示原始集中正数和负数的个数,格式如下:
“负数个数:a”和“正数个数:b”,其中 a 和 b 是未知的。

下面是一些示例:
a) 如果输入以下数字
2
5
6
则显示
负数个数:0
正数个数:3

b) 如果输入以下数字
-2
-5
6
则显示
负数个数:2
正数个数:1

要求:
  • 程序应从键盘读取这些数字。
  • 该程序应在屏幕上显示文本。
  • 程序应显示原始集中负数的个数。
  • 程序应显示原始集中正数的个数。
  • 如果没有负数,程序应显示“负数个数:0”。
  • 如果没有正数,程序应显示“正数个数:0”。
  • 请注意,0 既不是正数也不是负数。
package zh.codegym.task.task04.task0429; /* 正数和负数 */ import java.util.Scanner; public class Solution { public static void main(String[] args) throws Exception { //在此编写你的代码 Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); int a = compare1(x,y,z); int b = compare(x,y,z); System.out.println("负数的个数:"+a); System.out.println("正数的个数:"+b); } public static int compare (int x,int y,int z){ int positive = 0; if(x>0){ positive++; } if(y>0){ positive++; } if(z>0){ positive++; } return positive; } public static int compare1 (int x,int y,int z){ int negative = 0; if(x<0){ negative++; } if(y<0){ negative++; } if(z<0){ negative++; } return negative; } }
+1
评论 (5)
  • 受欢迎
  • 新
  • 旧
你必须先登录才能发表评论
canghailongheng
级别 12 ,Los Angeles,United States
30 十月 2022, 12:39
需要声明计数器,0也许变量,但是输入0,因为0+0还是0,没有变化。
0
canghailongheng
级别 12 ,Los Angeles,United States
30 十月 2022, 12:38
public class Solution {
    public static void main(String[] args) throws Exception {
        //在此编写你的代码
        //读取三个数字
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(reader.readLine());
        int b = Integer.parseInt(reader.readLine());
        int c = Integer.parseInt(reader.readLine());
        //正数和负数的计数器
        // na为正数
        int na = 0;
        // po为负数
        int po = 0;

        int zero = 0;
        if (a > 0) {
            na++;
        } else if (a == 0) {
            zero++;
        } else {
            po++;
        }

        if (b > 0) {
            na++;
        }else if (b == 0) {
            zero++;
        }else {
            po++;
        }

        if (c > 0) {
            na++;
        }else if (c == 0) {
            zero++;
        }else {
            po++;
        }
        System.out.println("负数个数:" + po);
        System.out.println("正数个数:" + na);
    }
}
0
ohh
级别 7 ,China,China
25 七月 2022, 14:36
package zh.codegym.task.task04.task0429; /* 正数和负数 */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //在此编写你的代码 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(br.readLine()); int b = Integer.parseInt(br.readLine()); int c = Integer.parseInt(br.readLine()); //声明数组并赋值 int[] nums = new int[]{a, b, c}; //正数个数 int positiveNumber = 0; //负数个性 int negativeNumber = 0; //foreach方式遍历数组 for (int num : nums) { if (num < 0) {//小于0时负数加1 negativeNumber++; } else if (num > 0) {//大于0时正数加1 positiveNumber++; } } //输出结果 System.out.println("正数个数:" + positiveNumber); System.out.println("负数个数:" + negativeNumber); } }
0
shadow
级别 22 ,Hangzhou,China
20 五月 2020, 11:14
输出多了个 “的” 吧
0
建飞 蒲
级别 6 ,Chengdu,China
21 五月 2020, 06:59
还真是 哈哈哈
0
了解
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • 游戏项目
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 活动
  • Affiliate Program
公司
  • 关于我们
  • 联系方式
  • 评论
  • 新闻发布室
  • CodeGym 教育
  • 常见问题
  • 支持
CodeGym CodeGym 是一个从零开始学习 Java 语言编程的在线课程。本课程是初学者掌握 Java 语言的绝佳方式。它包含 1200 多个可即时验证的任务,以及基本范围内的 Java 基础理论。为了帮助你在教育上取得成功,我们实现了一组激励功能:小测验、编码项目以及有关高效学习和 Java 语言开发人员职业方面的内容。
关注我们
界面语言
程序员不是天生的 © 2023 CodeGym
MastercardVisa
程序员不是天生的 © 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.