小白又来问问题了,谁能帮我找找错,输入12/0 的时候 还是true呢? 找了好久。。。

PcKing少 发布于 2013/08/24 15:01
阅读 193
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

package day02;


import java.util.Scanner;


public class LogicDemo {



public static void main(String[] args) {

char sex ;
while(true){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年龄:");
int age = scanner.nextInt();
System.out.println("请输入性别(0表示女,1表示男):");
int sexIs = scanner.nextInt();
if(sexIs == '0'){
sex = '女';
}else
{
sex = '男';
}
boolean x = check(age,sex);
if(x){
System.out.println(x+"是小男孩");
}else
System.out.println(x+"不是小男孩");
}
}


private static boolean check(int age, char sex) {
if(age<16&sex=='男'){
return true;
}else
return false;

}


}
加载中
0
情天大圣
情天大圣
import java.util.Scanner;

public class LogicDemo {
    public static void main(String[] args) {

        String sex;
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入年龄:");
            int age = scanner.nextInt();
            System.out.println("请输入性别(0表示女,1表示男):");
            int sexIs = scanner.nextInt();
            if (sexIs == 0) {
                sex = "女";
            } else {
                sex = "男";
            }
            boolean x = check(age, sex);
            if (x) {
                System.out.println(x + "是小男孩");
            } else
                System.out.println(x + "不是小男孩");
        }
    }

    private static boolean check(int age, String sex) {
        if (age < 16 && "男".equals(sex)) {
            return true;
        } else
            return false;

    }
}
0
漫步空中
漫步空中
该评论暂时无法显示,详情咨询 QQ 群:点此入群
PcKing少
PcKing少
哎呀 正解!!! 刚接触java有点吃力 太感谢了 都是我太马虎。。
0
伊藤熊吉
伊藤熊吉
我在年龄那里输入12/0 会报异常,因为除数不能为0,你是如果正确运行的?
情天大圣
情天大圣
回复 @伊藤熊吉 : 了解
伊藤熊吉
伊藤熊吉
回复 @情天大圣 : 嘛,我哪里抬杠了 = = 因为看到输入时想到的是什么都可以输入嘛,然后/看成除法了...一下子没想过来而已啊
PcKing少
PcKing少
回复 @情天大圣 : 是我的表达有错误,嘿嘿 别怪他
情天大圣
情天大圣
回复 @伊藤熊吉 : 晕,抬杠啊
伊藤熊吉
伊藤熊吉
回复 @情天大圣 : 看了,但是还是可以输入12/0的不是么:P
下一页
0
PcKing少
PcKing少

引用来自“大巧不工”的答案

sexIs 是int类型,所以应该把if(sexIs == '0')改成if(sexIs == 0)
厉害厉害
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部