扫描仪问题:没有此类元素异常

时间:2016-04-01 10:22:44

标签: java

我是Java的新手,我在方法userInput第16行遇到错误,它会抛出一个没有这样的元素异常。我需要能够在'之前传递两者的价值观。和'之后。'谢谢!这是代码:

import java.util.Scanner;
public class BasketballGame {
    public static String nameFirstTeam = "Bulldogs";
    public static String nameSecondTeam = "Wildcats";
    private static String before, character1, character2, after;

    public static int scoreFirstTeam = 0;
    public static int scoreSecondTeam = 0;
    public int scoreOne, scoreTwo, scoreThree;
    //determine end of game
    //System.out.println("Enter the team that scored ('a' for Bulldogs, 'b' for Wildcats), then enter the number of points scored.");
    public static void userInput(){

        Scanner keyboard = new Scanner(System.in);
        //keyboard.useDelimiter(" ");
        character1 = keyboard.next();
        before = String.valueOf(character1.charAt(0));
        //System.out.println(before); 
        character2 = keyboard.next();
        after = String.valueOf(character1.charAt(1));
        //System.out.println(after); 
    }       



    public static boolean finished(){
        if (scoreFirstTeam >= 50){
            return true;
        }
        else if (scoreSecondTeam >= 50){
            return true;
        }
        else{
            return false;}
    }
    public static void playGame(){

        while(!finished()){
            if (before== "a"){
                System.out.println("a");
            }
            if (after=="1"){
                System.out.println("1");
            }
            System.out.println("Enter a score.");
            userInput();
            if (before=="a" && after=="1"){
                scoreFirstTeam = (scoreFirstTeam +1);
                if (scoreFirstTeam > scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
                }
                else if (scoreFirstTeam < scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
                }
                else{
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
                }
            }
            else if (before=="a" && after=="2"){
                scoreFirstTeam = (scoreFirstTeam +2);
                if (scoreFirstTeam > scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
                }
                else if (scoreFirstTeam < scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
                }
                else{
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
                }
            }
            else if (before=="a" && after=="3"){
                scoreFirstTeam = (scoreFirstTeam +3);
                if (scoreFirstTeam > scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
                }
                else if (scoreFirstTeam < scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
                }
                else{
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
                }
            }
            else if (before=="b" && after=="1"){
                scoreSecondTeam = (scoreSecondTeam +1);
                if (scoreFirstTeam > scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
                }
                else if (scoreFirstTeam < scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
                }
                else{
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
                }
            }   
            else if (before=="b" && after=="2"){
                scoreSecondTeam = (scoreSecondTeam +2);
                if (scoreFirstTeam > scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
                }
                else if (scoreFirstTeam < scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
                }
                else{
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
                }
            }       
            else {
                scoreSecondTeam = (scoreSecondTeam +3);
                if (scoreFirstTeam > scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
                }
                else if (scoreFirstTeam < scoreSecondTeam){
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
                }
                else{
                    System.out.println(nameFirstTeam +  " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
                }
            }               
        }
        System.out.println("");
    }

    public static void main(String[] args){
        BasketballGame game = new BasketballGame();
        game.playGame();
        while (finished()){

            if (scoreFirstTeam > scoreSecondTeam){ 
                System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " win!");
            }
            else if (scoreFirstTeam < scoreSecondTeam){
                System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " win!");   
            }
            else{
                System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is a draw.");
            }
        }
    }
}

0 个答案:

没有答案