请问大家,我这哪错了?谢谢

adot111 2003-10-19 03:31:52
import javax.swing.*;
public class A2{
public static void main(String agrs[]){
int x;
int y=0;

String input = JOptionPane.showInputDialog("Enter a word, please");
String input1 = JOptionPane.showInputDialog("Enter a letter, please");

String newinput = input.toLowerCase();
String oldinput = input1.toLowerCase();

int A = newinput.length();
for(int i=0; i<A; i++){
if(oldinput == newinput.charAt(i)){
x=1;
y=y+x;}

}

JOptionPane.showMessageDialog(null,input1+"apears"+y+"times in this word"+input);
}
}

提示说(oldinput == newinput.charAt(i))
这个语句有问题,说是==不能比较这2个,但是我用equals 也不行啊
怎么回事,请大家指点
...全文
50 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
imagex 2003-10-19
  • 打赏
  • 举报
回复
import javax.swing.*;
public class test{
public static void main(String agrs[]){
int x;
int y=0;

String input = JOptionPane.showInputDialog("Enter a word, please");
String input1 = JOptionPane.showInputDialog("Enter a letter, please");

String newinput = input.toLowerCase();
String oldinput = input1.toLowerCase();

int A = newinput.length();
for(int i=0; i<A; i++){
if(oldinput == String.valueOf(newinput.charAt(i))){
x=1;
y=y+x;}

}

JOptionPane.showMessageDialog(null,input1+"apears"+y+"times in this word"+input);
}
}
skyboy0720 2003-10-19
  • 打赏
  • 举报
回复
charAt( )函数返回的是一个基本类型“char”
而你前面是一个String对象,肯定不能比较啊


oldinput == String.valueOf(newinput.charAt(i))或者
oldinput == String.toString(newinput.charAt(i))

试试



yangFrame 2003-10-19
  • 打赏
  • 举报
回复
""+newinput.charAt(i)
这样就把newinput.charAt(i)传成String
这样就能比较了
adot111 2003-10-19
  • 打赏
  • 举报
回复
是呀,他是提示不能比较
可是我的概念可能没有搞清楚,能说一下吗?
yangFrame 2003-10-19
  • 打赏
  • 举报
回复
应该是oldinput.equals(""+newinput.charAt(i))
字符串怎么能和char比较呢
adot111 2003-10-19
  • 打赏
  • 举报
回复
oldinput.equals(newinput.charAt(i)))
也不行啊
jscsqb 2003-10-19
  • 打赏
  • 举报
回复
oldinput.equals(newinput.charAt(i)))
stamp80 2003-10-19
  • 打赏
  • 举报
回复
oldinput是字符串,newinput。charAt得到的是字符,所以不能比较。我猜测是这样。

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧