无论我的输入如何重复

时间:2014-07-25 19:14:49

标签: java infinite-loop repeat

所以我想写的程序是这样的。对手随机选择一个数字,其中每个数字对应一个人。然后用户做同样的事情。然后输出是显示用户选择的内容,对手选择的内容以及赢得该匹配的人。它与岩石,纸剪刀类似,但增加了一些东西。我还必须显示比赛次数和用户获胜次数。但我的问题是这个。不管我的输入是什么,它一直说我做了一个无效的选择并再次选择它。这就是我所拥有的。

public static void main(String[] args) {
    int game=0;
    int won = 0; 
    int opponent;
    Random rand = new Random();
    System.out.println("Welcome to the game. Here are the rules:");        
    Scanner in = new Scanner(System.in);
    rules();
    choices();
    int choice = selected(in);
    while (choice !=0) {
        rules();
        choices();
        choice = selected(in);
        opponent = opp();
        result(choice, opponent, in);
        game+=1;
    }
    System.out.println("You played " +game+" games.");
    System.out.println("Quitting");
}
public static void rules(){
    System.out.println("\nFlurg beats Stuga and Kvartal");
    System.out.println("Stuga beats Kvartal");
    System.out.println("Kvartal beats Lerberg");
    System.out.println("Lerberg beats Flurg and Stuga");
    System.out.println("The computer wins in the event of a tie.");
    System.out.println();
}
public static int opp(){
    Random rand = new Random();
    int opponent = rand.nextInt(4)+1;
    return opponent;
}

public static void choices(){
    System.out.println("Please select your choice:");
    System.out.println("'1' for Flurg ");
    System.out.println("'2' for Stuga ");
    System.out.println("'3' for Kvartal ");
    System.out.println("'4' for Lerberg ");
    System.out.println("'0' to Quit ");
    System.out.print("Selection: ");
}

public static int selected(Scanner in){
    int choice = in.nextInt();
    while (choice != 1 && choice != 2 &&
           choice != 3 && choice != 4 &&
           choice != 0) {
        System.out.println("Invalid choice");
    choices();
    choice = in.nextInt();
}
    return choice;
}

public static int result(int choice, int opponent, Scanner in) {
    int won=0;
    System.out.print("You picked:" +choice+". Opponent picked:" +opponent+".");
    if(choice == 1 && (opponent == 2) || (opponent == 3)){
        System.out.println("You won!");
        won+=1;
    }
    else if(choice == 2 && opponent == 3){
        System.out.println("You won!");
        won+=1;
    }
    else if(choice == 3 && opponent == 4){
        System.out.println("You won!");
        won=+1;
    }
    else if(choice == 4 && (opponent == 1 || opponent == 2)){
        System.out.println("You won!");
        won+=1;
    }
    else {
        System.out.println("You lost!");
    }        
    return won;
}

}               

修改 所以我确切地解释了我对!=1部分所说的话。但它又在循环中。出于某种原因,它不想运行result()

EDIT#2 主要也已经解决了类似的问题。

EDIT#3 我根据我所说的内容添加了更多行。

3 个答案:

答案 0 :(得分:0)

您正在将找到的值与字符进行比较。

int choice = in.nextInt();
while (choice != '1' //...

但是,这会将输入的数值与字符'1'的字符的ASCII码进行比较。因此,您的循环将继续,直到您输入其中一个数字48(' 0')到52(' 4')。

您应该与数字进行比较:

while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0) {
    System.out.println("Invalid choice");
}

同样如此
while (choice !='0') {
    rules();
    choices();
    selected(in);   
    opp();
    result(choice, opponent, in);
}

在您的main方法中 - 将其更改为

while (choice != 0) { // change this to 0 instead of '0'
    rules();
    choices();
    choice = selected(in); // assign the value to choice
    opponent = opp(); // assign the value to opponent
    result(choice, opponent, in);
}

您也没有将方法的返回值分配给局部变量 - 它们永远不会改变,循环永远不会像那样结束!

答案 1 :(得分:0)

更改此

choice != '1' && choice != '2' &&
choice != '3' && choice != '4' &&
choice != '0'

到这个

choice != 1 && choice != 2 &&
choice != 3 && choice != 4 &&
choice != 0

你要做的是int!= char,ASCII中'1'的值是49所以它确实不是1.你正在从扫描仪读取int。

答案 2 :(得分:0)

问题出在这里

int choice = in.nextInt();
while (choice != '1' && choice != '2' &&
       choice != '3' && choice != '4' &&
       choice != '0') {
    System.out.println("Invalid choice");

nextInt()将返回int类型的十进制值。 (int)1!=(char)'1'。我能想到的最简单的解决方案是

while (choice != 1 && choice != 2 &&
       choice != 3 && choice != 4 &&
       choice != 0) {