![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
帮忙改一下java程序中的错误,在线等。。。。。谢谢
importjava.util.Scanner;publicclassEx3_1{publicstaticvoidmain(Strings[]args){Stringse...
import java.util.Scanner;
public class Ex3_1{
public static void main(Strings[] args){
String sentence = new String("The past is gone and static. Nothing we can do will change it.Thefuture is before us and dynamic. Everything we do will affect it.");
int a=sentence.length();
Scanner sc = new Scanner(System.in);
b = sc.next();
int c=0;
for(int i=0;i<=a;i++)
{char ch1=sentence.charAt(i);
char ch2=b;
if(ch1=ch2)
{c=c++;}
}
System.out.println("+b+出现的频率是"+c);
}
} 展开
public class Ex3_1{
public static void main(Strings[] args){
String sentence = new String("The past is gone and static. Nothing we can do will change it.Thefuture is before us and dynamic. Everything we do will affect it.");
int a=sentence.length();
Scanner sc = new Scanner(System.in);
b = sc.next();
int c=0;
for(int i=0;i<=a;i++)
{char ch1=sentence.charAt(i);
char ch2=b;
if(ch1=ch2)
{c=c++;}
}
System.out.println("+b+出现的频率是"+c);
}
} 展开
4个回答
展开全部
很明显的错误
main(Strings[] args){
改成
main(String[] args){
b = sc.next();
b这个变量没有类型。
main(Strings[] args){
改成
main(String[] args){
b = sc.next();
b这个变量没有类型。
追问
一定要写在他后面吗,我在后面写的,改了还是不行啊
追答
public static void main(String[] args){
String sentence = new String("The past is gone and static. Nothing we can do will change it.Thefuture is before us and dynamic. Everything we do will affect it.");
int a=sentence.length();
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查询的字符:");
String b = sc.next();
int c=0;
for(int i=0;i<a;i++){
char ch1=sentence.charAt(i);
// System.out.println(b+":"+String.valueOf(ch1)+":"+b.equals(String.valueOf(ch1)));
if(b.equals(String.valueOf(ch1))){
c++;
}
}
System.out.println(b+"出现的频率是"+c);
}
我给你改了一下。。
展开全部
public static void main(String[] args) {
String sentence = new String(
"The past is gone and static. Nothing we can do will change it.Thefuture is before us and dynamic. Everything we do will affect it.");
int a = sentence.length();
Scanner sc = new Scanner(System.in);
String b = sc.next();
int c = 0;
for (int i = 0; i < a; i++) {
String ch1 = sentence.substring(i, i+1);
// 搜索
if (ch1.equals(b)) {
c +=1;
}
}
System.out.println("+b+出现的频率是" + c);
}
String sentence = new String(
"The past is gone and static. Nothing we can do will change it.Thefuture is before us and dynamic. Everything we do will affect it.");
int a = sentence.length();
Scanner sc = new Scanner(System.in);
String b = sc.next();
int c = 0;
for (int i = 0; i < a; i++) {
String ch1 = sentence.substring(i, i+1);
// 搜索
if (ch1.equals(b)) {
c +=1;
}
}
System.out.println("+b+出现的频率是" + c);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Ex3_1
{
public static void main(String[] args)
{
String sentence = new String("The past is gone and static. Nothing we can do will change it.Thefuture is before us and dynamic. Everything we do will affect it.");
int a = sentence.length();
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字母:");
String b = sc.next();
int c = 0;
for (int i = 0; i < a; i++)
{
String str1=String.valueOf(sentence.charAt(i));
if (str1.equals(b))
{
c++;
}
}
System.out.println(b+"出现的频率是:" + c);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个是要实现查找出现次数吗?
追问
是的
追答
这段程序错误不少,比如b = sc.next();的b应该是String(next()的返回值),但是 char ch2=b赋值是类型不对; if(ch1=ch2)中应该是==;for(int i=0;i<=a;i++)中i=a,也就是String长度时,必然会抛出越界。好像c=c++也不太对,你可以试一下,可能c的值不变,这个和自增运算的优先度有关,一般不这么写。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询