Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

java - im getting an error and cannot find a way to solve it

I am creating a TictTacToe for my school project (object-orientated) but I ran into this problem where when I try to call the class I get an error saying "method placements in class placePiece can not be applied ton given types". I tried passing in char[][] and int into: new placePiece(char[][] GameBoard, int PlayerPiece); like this but then it gives me an error saying ";" expected and .class expected. Like I said Im new to programming so literally anything will help.

import java.util.Arrays;
import java.util.Scanner;


class Main {
  public static void main(String[] args) {
        GameBoard g = new GameBoard();
        g.print();
        
        SelectPlayingPiece s = new SelectPlayingPiece();
        s.Scan();

      PlacePiece p = new PlacePiece();
       p.placements(); 
  } 

import java.util.Arrays;
public class GameBoard{

 public static void print(){
//create a game board for the user to play.
    char[][] gameBoard = {{' ','|' ,' ', '|', ' '},
                          {'-','+' ,'-', '+', '-'},
                          {' ','|' ,' ', '|', ' '}, 
                          {'-','+' ,'-', '+', '-'}, 
                          {' ','|' ,' ', '|', ' '}};      
                          
     //print gameboard 2D array.
    for ( char[] row : gameBoard){
     for ( char c : row ){
        System.out.print(c);
          }
   System.out.println();
        }
      }         
 }

import java.util.Scanner;
import java.util.Arrays;

public class PlacePiece{
  public static void placements(char[][] gameBoard, int playerPiece){
    //game starts and player selects a column to place thier piece. 
    System.out.print("Game has begun,please enter where you would like to place (1~9):");
    Scanner scan = new Scanner(System.in);
      int placement = scan.nextInt();
  
          char piece = 'X';
          if( playerPiece == 1){
              piece = 'X';
              } else {
                piece = 'O';
            }

    switch(placement){
      case 1: gameBoard[0][0] = piece;
              break;
      case 2: gameBoard[0][2] = piece;
              break;
      case 3: gameBoard[0][4] = piece;
              break;
      case 4: gameBoard[2][0] = piece;
              break;
      case 5: gameBoard[2][2] = piece;
              break;
      case 6: gameBoard[2][4] = piece;
              break;
      case 7: gameBoard[3][0] = piece;
              break;
      case 8: gameBoard[3][2] = piece;
              break;
      case 9: gameBoard[3][4] = piece;
              break;
    }  

   }
   
}

import java.util.Scanner;

public class SelectPlayingPiece{  
    //allow players to pick thier playing piece.           
    
    public void Scan(){
      
      Scanner scan = new Scanner(System.in); 
      System.out.println("Welcome to TicTacToe! Frist, pick your playing piece: Type 1 to be X and type 0 to be O ");
      int playerPiece = scan.nextInt();
    
      char player1 = 'X';
      if( playerPiece == 1){
      System.out.print("player 1 is X!");
        player1 = 'X';
        } else{
       System.out.print("Player 1 is O!");
         player1 = 'O';
      } 
   }  
}






question from:https://stackoverflow.com/questions/65875809/im-getting-an-error-and-cannot-find-a-way-to-solve-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The placements method on PlacePiece takes char[][] and int as arguments. You're calling it without any arguments, which results in your first error.

You're then trying to pass these arguments not to the placements method, but the constructor of PlacePiece. Since The PlacePiece class only contains a default constructor (since you didn't explicitly provide one), this also fails.

Try replacing this part of your code:

PlacePiece p = new PlacePiece();
p.placements(); 

with something like this:

PlacePiece p = new PlacePiece();
p.placements(gameBoard, playerPiece);

You'll have to create a variable called gameBoard of type char[][] and a variable called playerPiece of type int before this obviously.

Unrelated to your problem, but look into static methods and how they're called. Since you're using only a static method on all of your classes, you could just do something like PlacePiece.placements(gameBoard, playerPiece) instead of creating an instance first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...