Topic: 菜鸟问题(未对自变量类型 char, boolean 定义运算符) |
Print this page |
1.菜鸟问题(未对自变量类型 char, boolean 定义运算符) | Copy to clipboard |
Posted by: sunyboy001 Posted on: 2005-08-16 19:25 这个题目是我学习的书(清华出的《java程序设计基础》)上的两个问题 题目:从键盘输入字母,如果是L,就显示Left;如果是R,就显示Right,其他的字母就显示Not Known。 我编的是: 1 import java.io.*; 2 public class Exercise3_3 3 { 4 public static void main(String[] args) 5 { 6 char c = 0; 7 System.out.print("Please enter a letter: "); 8 try{ 9 c=(char)System.in.read(); 10 }catch(IOException e){}; 11 if(c='L'||c='R') 12 if(c='R') 13 { 14 System.out.println("Right"); 15 } 16 else 17 System.out.println("Left"); 18 else 19 System.out.println("Not known"); 20 } 21 } 上面的是根据书上的一个例子来编的: 1 import java.io.IOException; 2 public class IfElse2 3 { 4 public static void main(String[] args) 5 { 6 char c = 0; 7 short i ='a'-'A'; 8 System.out.print("Enter a character: "); 9 try{ 10 c =(char)System.in.read(); 11 }catch(IOException e){}; 12 if(c>='A'&&c<='Z'||c>='a'&&c<='z') 13 if(c>='A'&&c<='Z'){ 14 c+=i; 15 System.out.println("It is a captal letter. The letter is: "+c); 16 } 17 else 18 System.out.println("It is a lower letter. The letter is: "+c); 19 else 20 System.out.println("It's not a letter."); 21 } 22 } 这个例题编译运行都没报错,但是我编译的那个就报错,我用的工具是eclipse,提示有个错误:标记“=”上有语法错误,应为 != Exercise3_3.java 第 11 行 我于是把"="改成"!="结果出现2个错误:1 :未对自变量类型 char, boolean 定义运算符 || Exercise3_3.java 第 11 行 2:类型不匹配:不能从 char 转换为 boolean Exercise3_3.java 第 12 行 这次没有更正可用,请问正确的写法是什么?为什么这么改?多谢了!!!! 请使用准确的文字描述作为标题 Your next post without a proper Subject will be removed. Original subject : 呵呵---菜鸟问题,望各位大哥大姐看看指正,谢谢了 |
2.Re:呵呵---菜鸟问题,望各位大哥大姐看看指正,谢谢了 [Re: sunyboy001] | Copy to clipboard |
Posted by: why Posted on: 2005-08-16 19:46 11 if(c='L'||c='R') 12 if(c='R') should be == , not = |
3.Re:菜鸟问题(未对自变量类型 char, boolean 定义运算符) [Re: sunyboy001] | Copy to clipboard |
Posted by: sunyboy001 Posted on: 2005-08-16 19:56 哦,试试看,谢谢了 |
4.Re:菜鸟问题(未对自变量类型 char, boolean 定义运算符) [Re: sunyboy001] | Copy to clipboard |
Posted by: sunyboy001 Posted on: 2005-08-16 20:01 恩,真的列。多谢多谢 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |