我写了个………
我是想让用户输入题目的对与错来判断,但是无论输入什么,程序总是执行 这一行
System.out.println("只只只 输出……");还有这个, 为什么我一用 = 就报错,什么格式不能转换的,但一用 != 就不报错,但却总执行上面那一行
纠结啊
if(judge!="0")
if(judge!="0"){//错误 System.out.println("只只只 输出……"); }else{//正确 System.out.println(" 输出……"); } 全部代码在下面 }
package hulande; import java .io.*; public class run { public static void main(String[] args) throws IOException { String [] question = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"}; System.out.println("正确请按enter直接跳过 ;"); System.out.println("错误请按 0 后回车"); for( int i=0;i<=18;i++){ //String [] question = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18……………………!!!结束!!!"}; System.out.println(question[i]); //输入 InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader(reader); String judge = input.readLine(); if(judge!="0"){//错误 System.out.println("只只只 输出……"); }else{//正确 System.out.println(" 输出……"); } } } }
if(judge!="0"),谁告诉你怎么用的?
if("0".equals(judge)){}
等号是比较内存地址,不是字符串的值,等号只能用来比较基本类型,不能用来判断对象是否相等
1.=是赋值,!=是比较,猜你想写的是==吧
2.java不是十分清楚,不知道judge读取的输入是否带有回车符号?
应该用“==”吧,如果是String类型的用equals版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。