簡體   English   中英

Java:使用 Static 方法在 Main 之外的方法中調用 Object

[英]Java: Calling Object in Method Outside of Main with Static Methods

應該很容易解決,但我想知道如何使用在 main.js 中創建的 object。 我試着說人類 h; 在 main (或任何其他方法)之外,所以我可以在其他方法中調用它,但是因為我使用的是 static 方法,所以我不能使用 h. 現在使用此代碼,java 告訴我找不到 h(在人工滾動方法中。)

import java.util.Scanner;

class Main
 {
  // static Human h;
  public static void main(String[] args) 
  {
    Scanner scan = new Scanner(System.in);
    Computer c = new Computer();
    Human h = new Human();
    System.out.println("Let's play PIG");
    System.out.println("What is your name");
    String name = scan.nextLine();
    System.out.println("Hello" + " " + name + " " + "you can go first");
    startGame(h, name); 
}
public static void startGame(Human h, String name)
{
Scanner scan = new Scanner(System.in); 
System.out.println("Press r to start your roll");
String rresponse = scan.nextLine();
if(!rresponse.equalsIgnoreCase("R"))
{
System.out.println("Try again");
startGame(h, name);
}
if(rresponse.equalsIgnoreCase("R"))
{
System.out.println("You pressed r, lets roll");
humanRoll(h, name);
}
}
public static void humanRoll(Human h, String name)
{
if(h.getRollNumh()==1)
{
System.out.println(name + " " + "you got a " + h.getRollNumh());
System.out.println("You got a score of" + h.getHumanTurnScore() + "this turn");
System.out.println("Here is your current overall score" + h.getHumanOverallScore());
System.out.println("Your rolled a one, now its the Computers' turn");
switchHuman();
}
}
 }

如果您需要查看 Human class 以提供上下文,我可以快速響應並添加它,但它只是實例變量、默認構造函數、重載構造函數、訪問器方法和修改器方法。

您可以將 Human 作為方法參數傳遞給 startGame 和 humanRoll 將它們定義為

public static void startGame(Human human) 
public static void humanRoll(Human human)

然后,在 main 中調用startGame(h)

看看這是否有效...

import java.util.Scanner;

class Main {

//tried to put Human h; up here but didn't work due to static methods 

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    Computer c = new Computer();
    Human h = new Human();
    System.out.println("Let's play PIG");
    System.out.println("What is your name");
    String name = scan.nextLine();
    System.out.println("Hello" + " " + name + " " + "you can go first");
    startGame(h);
  }

  public static void startGame(Human h) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Press r to start your roll");
    String rresponse = scan.nextLine();
    if (!rresponse.equalsIgnoreCase("R")) 
    {
      System.out.println("Try again");
      startGame(h);
    }
    if (rresponse.equalsIgnoreCase("R")) 
    {
      System.out.println("You pressed r, lets roll");
      humanRoll(h);
    }
  }

  public static void humanRoll(Human h) 
  {
//RollNumh is just the dice number that the player rolled 
    if (h.getRollNumh() == 1) 
    {
      System.out.println(name + " " + "you got a " + h.getRollNumh);
      System.out.println("You got a score of" + h.getHumanTurnScore() + "this turn");
      System.out.println("Here is your current overall score" + h.getOverallHumanOverallScore());
      System.out.println("Your rolled a one, now its the Computers' turn");
      switchHuman();
    }
  }

}

如果您希望 Human h 為 static(您的程序中只有一個),那么您可以這樣定義它:

static Human h;

public static void main(...) {
...

在 Java 中,在 function 內聲明的變量只能在 function 內使用。 我們說變量的 scope 只在 function 之內。

修復:您可以將變量 h 作為參數傳遞給其他函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM