我目前正在尝试编写一个使用另一个方法的方法,该方法又从另一个类构造函数创建一个新对象。但我不知道能不能得到我创建的对象。当我尝试在方法中打印对象(使用创建对象的方法)时,它告诉我对象未定义。我对如何解决这个问题很困惑。我有这个其他方法的原因是因为在一个地方有太多的代码,所以我试图让它更清洁。 无论如何,我决定发布两个代码。第一个是我尝试让它们更短更容易让你们阅读。但也许,也许它可能没有我在原始代码中遗漏的东西。所以我发布了那个和我的构造函数类。希望它适合这里! 任何帮助将不胜感激!
以下是我尝试做的第一个“示例”示例:
public static void GameStart(){
//The print method
if (Player1){
playerVäljKort(true);
int val = readInt("Spara val = 1\nRedigera val = 2");
if (val == 2){
//This is where I am trying to print the object. I've tried doing this in several ways.
System.out.println(player1Kortlek());
}
}
}
public static void playerVäljKort(boolean playerTurn){
//I fail to define this outside the method.
Kortlek player1Kortlek = new Kortlek(kort)
Public class Kortlek{
private static int NewKortlek
public Kortlek(int kort){
NewKortlek = kort;
}
Public String toString(){
return "Number of cards = " + Kortlek + ".";
}
}
因此,如果这看起来完全正确,也许在我的真实代码中有一些我看不到的东西。希望这不会杀死整个网页。 这是第二个真正的代码示例尝试执行与上面相同的操作: 构造函数类:
public class Kortlek {
private static int KortlekRedCard1;
private static int KortlekRedCard2;
private static int KortlekRedCard3;
private static int KortlekRedCard4;
private static int KortlekRedCard5;
//private static int KortlekredCard6;
private static int KortlekYellowCard1;
private static int KortlekYellowCard2;
private static int KortlekYellowCard3;
private static int KortlekYellowCard4;
private static int KortlekYellowCard5;
private static int KortlekYellowCard6;
private static int KortlekBlueCard1;
private static int KortlekBlueCard2;
private static int KortlekBlueCard3;
private static int KortlekBlueCard4;
private static int KortlekBlueCard5;
//private static int KortlekblueCard6;
private static int KortlekBear;
private static int KortlekAmmo;
private static int KortlekNuclear;
private static int KortlekHeal;
private static int KortlekSkip;
private static int KortlekTank;
private static int KortlekLandmina;
private static int KortlekSteal;
public Kortlek(int redCard1, int redCard2, int redCard3, int redCard4, int redCard5,
int yellowCard1, int yellowCard2, int yellowCard3, int yellowCard4, int yellowCard5, int yellowCard6,
int blueCard1, int blueCard2, int blueCard3, int blueCard4, int blueCard5,
int bear, int ammo, int nuclear, int heal, int skip, int tank, int landmina, int steal){
this.KortlekRedCard1 = redCard1;
this.KortlekRedCard2 = redCard2;
this.KortlekRedCard3 = redCard3;
this.KortlekRedCard4 = redCard4;
this.KortlekRedCard5 = redCard5;
//this.KortlekredCard6 = redCard6;
this.KortlekYellowCard1 = yellowCard1;
this.KortlekYellowCard2 = yellowCard2;
this.KortlekYellowCard3 = yellowCard3;
this.KortlekYellowCard4 = yellowCard4;
this.KortlekYellowCard5 = yellowCard5;
this.KortlekYellowCard6 = yellowCard6;
this.KortlekBlueCard1 = blueCard1;
this.KortlekBlueCard2 = blueCard2;
this.KortlekBlueCard3 = blueCard3;
this.KortlekBlueCard4 = blueCard4;
this.KortlekBlueCard5 = blueCard5;
//KortlekblueCard6 = blueCard6;
this.KortlekBear = bear;
this.KortlekAmmo = ammo;
this.KortlekNuclear = nuclear;
this.KortlekHeal = heal;
this.KortlekSkip = skip;
this.KortlekTank = tank;
this.KortlekLandmina = landmina;
this.KortlekSteal = steal;
/*if (redCard1 > 4 || redCard2 > 4 || redCard3 > 4 || redCard4 > 4 || redCard5 > 4 ||
yellowCard1 > 4 || yellowCard2 > 4 || yellowCard3 > 4 || yellowCard4 > 4 || yellowCard5 > 4 ||
yellowCard6 > 4 || blueCard1 > 4 || blueCard2 > 4 || blueCard3 > 4 || blueCard4 > 4 || blueCard5 > 4 ||
specialCardBear > 4 || specialCardAmmo > 4 || specialCardNuclear > 8 ||
specialCardHeal > 8 || specialCardSkip > 10 || specialCardTank > 4 || specialCardBomb > 10 ||
specialCardSteal > 8 /* || KortlekNumber > 50*//*){
System.out.println("Du tog för många kort av någon sort.");
}*/
}
/*public static void redCard(){
KortlekredCard1;
}*/
/*
* Kortnamn
* Pistol
* Hagelgevär
* Granat(non-Javadoc)
* Granatkastare
* Bazooka
*
* Air Gun
* Lastersiktare
* Hagelbössa
* Maskingevär
* Kulspruta
* Sniper
*
* Käpp
* Strykjärn
* Stekpanna
* Goldklubba
* Järnstövlar
*
* Bear
* Ammo
* Nuclear
* Heal
* Skip
* Tank
* Landmina
* Steal
* @see java.lang.Object#toString()
*/
public String toString(){
return "1st Rött kort Pistol = " + KortlekRedCard1 + "/4\n2nd Rött kort Hagelgevär = " + KortlekRedCard2
+ "/4\n3rd Rött kort Granat = " + KortlekRedCard3 + "/4\n4th Rött kort Granatkastare = "
+ KortlekRedCard4 + "/4\n5th Rött kort Bazooka = " + KortlekRedCard5 + "/4\n1st Gult kort Air Gun = "
+ KortlekYellowCard1 + "/4\n2st Gult kort Lasersiktare = " + KortlekYellowCard2
+ "/4\n3rd Gult kort Hagelbössa = " + KortlekYellowCard3 + "/4\n4th Gult kort Maskingevär = "
+ KortlekYellowCard4 + "/4\n5th Gult kort Kulspruta = " + KortlekYellowCard5
+ "/4\n6th Gult kort Sniper = " + KortlekYellowCard6 + "/4\n1st Blått kort Käpp = "
+ KortlekBlueCard1 + "/4\n2nd Blått kort Strykjärn = " + KortlekBlueCard2 + "/4\n3rd Blått kort Stekpanna = "
+ KortlekBlueCard3 + "/4\n4th Blått kort Golfklubba = " + KortlekBlueCard4
+ "/4\n5th Blått kort Järnstövlar = " + KortlekBlueCard5 + "/4\n1st Special kort Bear = "
+ KortlekBear + "/4\n2nd Special kort Ammo = " + KortlekAmmo + "/8\n3rd Special kort Nuclear = "
+ KortlekNuclear + "/8\n4th Special kort Heal = " + KortlekHeal + "/8\n5th Special kort Skip = "
+ KortlekSkip + "/10n6th Special kort Tank = " + KortlekTank + "/4n7th Special kort Landmina = "
+ KortlekLandmina + "/10\n8th Special kort Steal = " + KortlekSteal + "/8\n";
}
}
印刷品/主要类别:
import java.util.Scanner;
public class KortMain {
//public static boolean Player1;
//public static boolean Player2;
private static Scanner userInput = new Scanner(System.in);
public static String readString(String str){
System.out.print(str);
return userInput.nextLine();
}
public static int readInt(String str){
System.out.print(str);
int i = userInput.nextInt();
userInput.nextLine();
return i;
}
/*public static void playerStats(){
if (Hp == 0 || Kort == 0){
System.out.println("You lose!");
}
}*/
public static void playerVäljKort(boolean playerTurn){
//boolean kortObjekt = true;
int kortCounter = 0;
int redCard1 = 0;
int redCard2 = 0;
int redCard3 = 0;
int redCard4 = 0;
int redCard5 = 0;
int yellowCard1 = 0;
int yellowCard2 = 0;
int yellowCard3 = 0;
int yellowCard4 = 0;
int yellowCard5 = 0;
int yellowCard6 = 0;
int blueCard1 = 0;
int blueCard2 = 0;
int blueCard3 = 0;
int blueCard4 = 0;
int blueCard5 = 0;
int bear = 0;
int ammo = 0;
int nuclear = 0;
int heal = 0;
int skip = 0;
int tank = 0;
int landmina = 0;
int steal = 0;
String[] kortVal = {"Välj 50 kort.\n\n1st Rött kort Pistol (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n2nd Rött kort Hagelgevär (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n3rd Rött kort Granat (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n4th Rött kort Granatkastare (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n5th Rött kort Bazooka (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n1st Gult kort Air Gun (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n2st Gult kort Lasersiktare (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n3rd Gult kort Hagelbössa (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n4th Gult kort Maskingevär (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n5th Gult kort Kulspruta (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n6th Gult kort Sniper (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n1st Blått kort Käpp (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n2nd Blått kort Strykjärn (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n3rd Blått kort Stekpanna (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n4th Blått kort Golfklubba (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n5th Blått kort Järnstövlar (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n1st Special kort Bear (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n2nd Special kort Ammo (Max 8): ",
"Du har hitills valt " + kortCounter + " kort.\n3rd Special kort Nuclear (Max 8): ",
"Du har hitills valt " + kortCounter + " kort.\n4th Special kort Heal (Max 8): ",
"Du har hitills valt " + kortCounter + " kort.\n5th Special kort Skip (Max 10): ",
"Du har hitills valt " + kortCounter + " kort.\n6th Special kort Tank (Max 4): ",
"Du har hitills valt " + kortCounter + " kort.\n7th Special kort Landmina (Max 10): ",
"Du har hitills valt " + kortCounter + " kort.\n8th Special kort Steal (Max 8): "};
for (int kortLoop = 0; kortLoop > kortVal.length; kortLoop++){
switch (kortLoop){
case 0:
redCard1 = readInt(kortVal[kortLoop]);
kortCounter += redCard1;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 1:
redCard2 = readInt(kortVal[kortLoop]);
kortCounter += redCard2;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 2:
redCard3 = readInt(kortVal[kortLoop]);
kortCounter += redCard3;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 3:
redCard4 = readInt(kortVal[kortLoop]);
kortCounter += redCard4;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 4:
redCard5 = readInt(kortVal[kortLoop]);
kortCounter += redCard5;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 5:
yellowCard1 = readInt(kortVal[kortLoop]);
kortCounter += yellowCard1;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 6:
yellowCard2 = readInt(kortVal[kortLoop]);
kortCounter += yellowCard2;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 7:
yellowCard3 = readInt(kortVal[kortLoop]);
kortCounter += yellowCard3;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 8:
yellowCard4 = readInt(kortVal[kortLoop]);
kortCounter += yellowCard4;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 9:
yellowCard5 = readInt(kortVal[kortLoop]);
kortCounter += yellowCard5;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 10:
yellowCard6 = readInt(kortVal[kortLoop]);
kortCounter += yellowCard6;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 11:
blueCard1 = readInt(kortVal[kortLoop]);
kortCounter += blueCard1;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 12:
blueCard2 = readInt(kortVal[kortLoop]);
kortCounter += blueCard2;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 13:
blueCard3 = readInt(kortVal[kortLoop]);
kortCounter += blueCard3;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 14:
blueCard4 = readInt(kortVal[kortLoop]);
kortCounter += blueCard4;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 15:
blueCard5 = readInt(kortVal[kortLoop]);
kortCounter += blueCard5;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 16:
bear = readInt(kortVal[kortLoop]);
kortCounter += bear;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 17:
ammo = readInt(kortVal[kortLoop]);
kortCounter += ammo;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 18:
nuclear = readInt(kortVal[kortLoop]);
kortCounter += nuclear;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 19:
heal = readInt(kortVal[kortLoop]);
kortCounter += heal;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 20:
skip = readInt(kortVal[kortLoop]);
kortCounter += skip;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 21:
tank = readInt(kortVal[kortLoop]);
kortCounter += tank;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 22:
landmina = readInt(kortVal[kortLoop]);
kortCounter += landmina;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
case 23:
steal = readInt(kortVal[kortLoop]);
kortCounter += steal;
if (kortCounter >= 50){
kortLoop = kortVal.length;
}
break;
}
}
if (playerTurn){
Kortlek player1Kortlek = new Kortlek(redCard1, redCard2, redCard3, redCard4, redCard5, yellowCard1,
yellowCard2, yellowCard3, yellowCard4, yellowCard5, yellowCard6, blueCard1, blueCard2,
blueCard3, blueCard4, blueCard5, bear, ammo, nuclear, heal, skip, tank, landmina, steal);
}
else{
Kortlek player2Kortlek = new Kortlek(redCard1, redCard2, redCard3, redCard4, redCard5, yellowCard1,
yellowCard2, yellowCard3, yellowCard4, yellowCard5, yellowCard6, blueCard1, blueCard2,
blueCard3, blueCard4, blueCard5, bear, ammo, nuclear, heal, skip, tank, landmina, steal);
}
}
public static void GameStart(){
boolean Player1 = true;
boolean Player2 = false;
if (Player1){
playerVäljKort(true);
int val = readInt("Spara val = 1\nRedigera val = 2");
if (val == 2){
System.out.println(player1Kortlek());
}
}
}
public static void main(String[] args){
GameStart();
}
}
答案 0 :(得分:-1)
似乎是player1Kortlek是一个Kortlek对象,但不是一种方法。
使用println时,它将调用object的toString方法,
所以你需要:
1)正确覆盖Kortlek的toString方法
2)使用System.out.println( player1Kortlek )显示信息;