刚编的java程序,在eclipse里没有显示语法错误,可是就是运行不了
packageorg.lxh.demo;classOperate{privatestaticintjishu1=0;privatestaticintjishu2=0;pr...
package org.lxh.demo;
class Operate{
private static int jishu1=0;
private static int jishu2=0;
private String a;
private char b[]=a.toCharArray();
public Operate(String a){
this.a=a;
}
public void jiSuan(){
for(int i=0;i<b.length;i++){
if(b[i]=='n'){
jishu1++;
}
if(b[i]=='o'){
jishu2++;
}
}
}
public int getJiShu1(){
return jishu1;
}
public int getJiShu2(){
return jishu2;
}
}
public class Xiti2 {
public static void main(String args[]){
Operate ope=new Operate("want you to know one thing");
System.out.println("n的个数是 "+ope.getJiShu1());
System.out.println("o的个数是 "+ope.getJiShu2());
}
}
忘记写ope.jiSuan();了,还是运行不了 展开
class Operate{
private static int jishu1=0;
private static int jishu2=0;
private String a;
private char b[]=a.toCharArray();
public Operate(String a){
this.a=a;
}
public void jiSuan(){
for(int i=0;i<b.length;i++){
if(b[i]=='n'){
jishu1++;
}
if(b[i]=='o'){
jishu2++;
}
}
}
public int getJiShu1(){
return jishu1;
}
public int getJiShu2(){
return jishu2;
}
}
public class Xiti2 {
public static void main(String args[]){
Operate ope=new Operate("want you to know one thing");
System.out.println("n的个数是 "+ope.getJiShu1());
System.out.println("o的个数是 "+ope.getJiShu2());
}
}
忘记写ope.jiSuan();了,还是运行不了 展开
4个回答
展开全部
这个是改后的类
class Operate {
private static int jishu1 = 0;
private static int jishu2 = 0;
private String a;
public Operate(String a) {
this.a = a;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == 'n') {
jishu1++;
}
if (a.charAt(i) == 'o') {
jishu2++;
}
}
}
public int getJiShu1() {
return jishu1;
}
public int getJiShu2() {
return jishu2;
}
}
你的错误在于private char b[]=a.toCharArray();这句。在数组b里面存放的知识a的地址而已。
相当于对于数组b根本还没有定义。所以报错
class Operate {
private static int jishu1 = 0;
private static int jishu2 = 0;
private String a;
public Operate(String a) {
this.a = a;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == 'n') {
jishu1++;
}
if (a.charAt(i) == 'o') {
jishu2++;
}
}
}
public int getJiShu1() {
return jishu1;
}
public int getJiShu2() {
return jishu2;
}
}
你的错误在于private char b[]=a.toCharArray();这句。在数组b里面存放的知识a的地址而已。
相当于对于数组b根本还没有定义。所以报错
展开全部
这样就可以了:
class Operate {
private static int jishu1 = 0;
private static int jishu2 = 0;
private String a;
private char b[];
public Operate(String a) {
this.a = a;
this.b=a.toCharArray();
}
public void jiSuan() {
for (int i = 0; i < b.length; i++) {
if (b[i] == 'n') {
jishu1++;
}
if (b[i] == 'o') {
jishu2++;
}
}
}
public int getJiShu1() {
return jishu1;
}
public int getJiShu2() {
return jishu2;
}
}
public class Xiti2 {
public static void main(String args[]) {
Operate ope = new Operate("want you to know one thing");
ope.jiSuan();
System.out.println("n的个数是 " + ope.getJiShu1());
System.out.println("o的个数是 " + ope.getJiShu2());
}
}
class Operate {
private static int jishu1 = 0;
private static int jishu2 = 0;
private String a;
private char b[];
public Operate(String a) {
this.a = a;
this.b=a.toCharArray();
}
public void jiSuan() {
for (int i = 0; i < b.length; i++) {
if (b[i] == 'n') {
jishu1++;
}
if (b[i] == 'o') {
jishu2++;
}
}
}
public int getJiShu1() {
return jishu1;
}
public int getJiShu2() {
return jishu2;
}
}
public class Xiti2 {
public static void main(String args[]) {
Operate ope = new Operate("want you to know one thing");
ope.jiSuan();
System.out.println("n的个数是 " + ope.getJiShu1());
System.out.println("o的个数是 " + ope.getJiShu2());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
注意作用域范围
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么个运行不了?有个异常栈拿来看看?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询