K
K
KirillIvanov282015-11-01 16:57:16
Java
KirillIvanov28, 2015-11-01 16:57:16

I decided to make a game program, but there is an error somewhere ... How to fix it?

The code

public class Player {
    byte number;
    String name;
    byte score;
}

import java.util.Scanner;


public class Game {
        
        Player player1 = new Player();
        Player player2= new Player();
        Player player3= new Player();     
        Scanner scanner = new Scanner(System.in);
        
        byte random;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Добро пожаловать в игру Super_Random");
        GoGame();
        }
    public static void GoGame(){
        Game game = new Game();
        
        game.WhatName();
        game.Random();
        game.First();
        game.Score();
        game.Second();
        game.Score();
        game.Fird();
        game.Score();
        game.result();
        
        Game.GoGame();
    }
    public void WhatName(){    
        System.out.println("Введите имя первого игрока...");
        player1.name = scanner.next();
        System.out.println("Введите имя второго игрока...");
        player2.name= scanner.next();
        System.out.println("Введите имя третьего игрока...");
        player3.name= scanner.next();
        scanner.close();
    }
    public void Random(){
        random = (byte)(Math.random()*9);
    }
    public void First(){
        System.out.println("Игрок "+ player1.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player1.number=scanner.nextByte();
        } 
        else{
            System.out.println("Игрок "+player1.name+" проиграл, его число не 0..9");
            player1.number = 11;
        }
       
        System.out.println("Игрок "+ player2.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player2.number=scanner.nextByte();
        } 
        else{
            System.out.println("Игрок "+player2.name+" проиграл, его число не 0..9");
            player2.number = 11;
        }
       
        System.out.println("Игрок "+ player3.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player2.number = 11;
        } 
        else{
            System.out.println("Игрок "+player3.name+" проиграл, его число не 0..9");
            player3.number = 11;
        }
        scanner.close();
    }
    public void Score(){
        if(player1.number==random){
            player1.score += 1; 
        } 
        if(player2.number==random){
            player2.score += 1; 
        } 
        if(player3.number==random){
            player3.score += 1; 
        } 
    }
    public void Second(){
        System.out.println("Игрок "+ player2.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player2.number=scanner.nextByte();
        } 
        else{
            System.out.println("Игрок "+player2.name+" проиграл, его число не 0..9");
            player2.number = 11;
        }
        
        System.out.println("Игрок "+ player3.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player3.number=scanner.nextByte();
        } 
        else{
            System.out.println("Игрок "+player3.name+" проиграл, его число не 0..9");
            player3.number = 11;
        }
        
        System.out.println("Игрок "+ player1.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player1.number = 11;
        } 
        else{
            System.out.println("Игрок "+player1.name+" проиграл, его число не 0..9");
            player1.number = 11;
        }
        scanner.close();
    }
    public void Fird(){
        System.out.println("Игрок "+ player3.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player3.number=scanner.nextByte();
        } 
        else{
            System.out.println("Игрок "+player1.name+" проиграл, его число не 0..9");
            player3.number = 11;
        }
        
        System.out.println("Игрок "+ player1.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player1.number=scanner.nextByte();
        } 
        else{
            System.out.println("Игрок "+player1.name+" проиграл, его число не 0..9");
            player1.number = 11;
        }
        
        System.out.println("Игрок "+ player2.name+" вводит число от 0 до 9...");
        if(scanner.hasNextByte()){
            player2.number = 11;
        } 
        else{
            System.out.println("Игрок "+player2.name+" проиграл, его число не 0..9");
            player2.number = 11;
        }
        scanner.close();
    }
    public void result(){
        System.out.println(player1.name + " набрал "+player1.score+" очков");
        System.out.println(player2.name + " набрал "+player2.score+" очков");
        System.out.println(player3.name + " набрал "+player3.score+" очков");
    }
}

Running debug itself
:
Welcome to the game Super_Random
Enter the name of the first player...

a
Enter the name of the second player...
b
Enter the name of the third player...
c
Player a enters a number between 0 and 9...
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.hasNext(Scanner.java:1433)
at java.util.Scanner.hasNextByte(Scanner.java :1810)
at java.util.Scanner.hasNextByte(Scanner.java:1795)
at SuperRandom.Game.First(Game.java:48)
at SuperRandom.Game.GoGame(Game.java:24)
at SuperRandom.Game.main (Game.java:17)
Java Result: 1

ASSEMBLY COMPLETED SUCCESSFULLY (total time: 10 seconds)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question