键盘输入在一个单独的类?

时间:2010-10-11 19:15:54

标签: java input keyboard user-input java.util.scanner

大约一周前开始学习Java。我制作了一个Java程序,用于计算评论的数量,以及某些内容的平均评论评分。例如,在应用程序商店,您将输入1星,2星,3星,4星和5星评论,该程序将计算平均评论分数。无论如何,也许它会更容易向您展示。我想要做的是将键盘输入部分放在不同的类中。但是当我尝试这样做时,编译器说“找不到符号”我知道Java中没有全局变量,但肯定有办法将用户输入变量传递给另一个类(?)。我尝试过“extends”关键字使我的键盘类成为子类,但这也无效。我基本上需要访问 firStar secStar thrSstar fouStar fivStar reviewHandler 类中的变量,这些变量最初是在 keyboardInput 类中定义的。

reviewHandler类:

public class reviewHandler {

public void reviews() {

keyboardInput keyboard = new keyboardInput();
keyboard.fiveStar();

int oneStar = firStar;
int twoStar = secStar;
int threeStar = thrStar;
int fourStar = fouStar;
int fiveStar = fivStar;

int starOne = oneStar * 1;
int starTwo = twoStar * 2;
int starThree = threeStar * 3;
int starFour = fourStar * 4;
int starFive = fiveStar * 5;

int reviewCount = oneStar + twoStar + threeStar + fourStar + fiveStar;

double reviewStarCount = starOne + starTwo + starThree + starFour + starFive;

double reviewAverage = reviewStarCount / reviewCount; 

reviewAverage = reviewAverage * 100;
reviewAverage = Math.round(reviewAverage);
reviewAverage = reviewAverage / 100;

System.out.println("Total reviews: " + reviewCount);
System.out.println("Average review score: " + reviewAverage);
}
}

keyboardInput class:

import java.util.Scanner; 

public class keyboardInput extends reviewHandler {

public void fiveStar() {

Scanner stars = new Scanner(System.in);
int firStar, secStar, thrStar, fouStar, fivStar; 
System.out.println("Number of 1 star reviews: ");
firstar = stars.nextInt();
System.out.println("Number of 2 star reviews: ");
secstar = stars.nextInt();
System.out.println("Number of 3 star reviews: ");
thrstar = stars.nextInt();
System.out.println("Number of 4 star reviews: ");
foustar = stars.nextInt();
System.out.println("Number of 5 star reviews: ");
fivstar = stars.nextInt();

}
}

reviewLauncher class:

public class reviewLauncher {

public static void main (String[] args) {
reviewHandler start = new reviewHandler();
start.reviews();

}
}

编辑:如果我将firStar,secStar,thrSstar,fouStar和fivStar变量移出方法,将它们转换为实例变量,程序编译并运行,但程序将返回0,当我尝试使用它时。

1 个答案:

答案 0 :(得分:0)

好的,通过修改后的问题,我想我已经对你想做的事情有了更好的了解。

这个程序正在尖叫的是一个帮助类来保存聚合评论信息。像这样:

public class ReviewInfo {
    public int numFiveStars = 0;
    public int numFourStars = 0;
    public int numThreeStars = 0;
    public int numTwoStars = 0;
    public int numOneStars = 0;
}

它为您提供了一个很好的整齐的捆绑包,您可以在其中保存您从键盘输入中读取的所有数据。

接下来,更新您的keyboardInput类,以便fiveStar()方法将您的所有数据存储在ReviewInfo对象中,并将该对象返回给调用代码:

import java.util.Scanner; 

public class keyboardInput extends reviewHandler {

    public ReviewInfo fiveStar() {
        Scanner stars = new Scanner(System.in);
        ReviewInfo info = new ReviewInfo();
        System.out.println("Number of 1 star reviews: ");
        info.numOneStars = stars.nextInt();
        System.out.println("Number of 2 star reviews: ");
        info.numTwoStars = stars.nextInt();
        System.out.println("Number of 3 star reviews: ");
        info.numThreeStars = stars.nextInt();
        System.out.println("Number of 4 star reviews: ");
        info.numFourStars = stars.nextInt();
        System.out.println("Number of 5 star reviews: ");
        info.numFiveStars = stars.nextInt();

        return info;
    }
}

现在你可以简化reveiwHandler.reviews()方法,以便它使用ReviewInfo助手类:

...
keyboardInput keyboard = new keyboardInput();
ReviewInfo info = keyboard.fiveStar();
int oneStar = info.numOneStars;
int twoStar = info.numTwoStars;
int threeStar = info.numThreeStars;
int fourStar = info.numFourStars;
int fiveStar = info.numFiveStars;
...

从那里你很高兴,我想。

在您的问题中,您询问如何在类之间共享变量。有办法做到这一点(静态变量,内部类和各种其他技术),但它几乎总是一个坏主意。如果你正在上课或读书,我相信你已经听过/读过“封装”一词。类之间共享的全局变量或变量会破坏封装,如果依赖它们,则存在创建难以处理或使用的代码的风险。 Globals通常不会对像这样的小项目造成麻烦,但对于任何更大的项目,它们都会很快烧毁你。

我希望这能回答你的问题。祝你好运!

-Chris