【问题标题】:Problems with Item/String getting input? [duplicate]项目/字符串获取输入的问题? [复制]
【发布时间】:2015-11-15 16:18:03
【问题描述】:

对不起,如果我没有意义,我没有太多练习

您好,我已将 Items 设置为保存一组字符,这些字符旨在放置在基于文本的地图上,但是当我要求 char 时,出现一个错误,指出它必须是字符串类型。有没有办法将我的项目转换为字符串,或允许输入项目?提前致谢

错误是用户尝试输入符号的地方。我知道我可以使用 string.valueof,但是我不确定在哪里实现它。

//imports
import java.util.Scanner;
import java.util.Random;


public class main {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        ABug[] BugObj = new ABug[4]; //Creating object BugObj of class ABug
        int loop = 4;
        int i = 0;
        int cycles;
        MyClass worldnew = new MyClass();

        System.out.println("Please enter the number of cycles you wish to run:");   
        cycles = reader.nextInt(); //getting the amount of cycles to be run
        System.out.print("____Current World____\n\n");
        worldnew.printWorld(); //calling method to print out world

        System.out.println("____Key____\n_F_ - Food\n_O_ - Object\n_ _ - Space\nSymbol - Bug");


        do{
            BugObj[i] = new ABug();  //creating instance

            System.out.print("Please enter the symbol which you wish to represent the bug:");
            BugObj[i].symbol = reader.next();
            System.out.print("Please enter the name of the bug:");
            BugObj[i].name = reader.next(); 
            System.out.println("Please enter the species of the bug:");   
            BugObj[i].species = reader.next(); 
            System.out.println("Please enter the horizontal position of the bug:");   
            BugObj[i].horpos = reader.nextInt();
            System.out.println("Please enter the vertical postion of the bug:");   
            BugObj[i].vertpos = reader.nextInt();   

            System.out.println("_______________ Bug " +(+i+1) + " _______________\n" );
            System.out.println("Symbol: " + BugObj[i].symbol);     //Printing bug information out
            System.out.println("Name: " + BugObj[i].name);           
            System.out.println("Species: " + BugObj[i].species);
            System.out.println("Horizontal Position: " + BugObj[i].horpos);
            System.out.println("Vertical Postion: " + BugObj[i].vertpos + "\n\n");
            move(BugObj[i], worldnew);

            i++;
            System.out.println("Would you like to enter another bug? \n 0-No,  1-Yes\n");
            loop = reader.nextInt();
        }while(loop == 1);
    }


    public static void move(ABug bug, MyClass worldnew){
        Scanner reader = new Scanner(System.in);
        System.out.println("Would you like this bug to move?\n 0-No,  1-Yes\n");
        if (reader.nextInt() == 0)
        {
            System.exit(0);
        }

        int x = bug.horpos;
        int y = bug.vertpos;
        Item a = bug.symbol;

        worldnew.setMap(x,y,bug.symbol);
        worldnew.printWorld();

        //get corordinate of bug
        //set map[coor] = symbol
        //print out map
        //int r = (int) (Math.random() * (2- -2)) + -2;
        //int originalHorpos = bug.horpos;
        //int originalVertpos = bug.vertpos;
        //bug.horpos = originalHorpos + r;
        //bug.vertpos = originalVertpos + r;


        //bug.horpos += r; 
        //bug.vertpos += r;

        System.out.println("New Horizontal Position: " +bug.horpos );
        System.out.println("New Vertical Postion: " +bug.vertpos);

    }
}


enum Item {
    OBJECT ('O'),FOOD ('F'), SPACE (' ');

    private final char symbol;
    Item(char symbol) {
        this.symbol = symbol;
    }
    char getSymbol() { return symbol; }
}
class MyClass {

    public void setMap(int x, int y, Item symbol)
    {
        this.map[x][y] = symbol;
    }
    Item[][] map = new Item[15][25];
    public void printWorld() {

        int v, h; //v - vert, h - hor

        for (v=1; v<=15; ++v)
        {
            for (h=1; h<=25; ++h)
            {


                final Item[] items = {Item.OBJECT, Item.FOOD, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE};
                Random random = new Random();
                int selection = random.nextInt(items.length);
                map[v-1][h-1] = items[selection];
                System.out.print(map[v-1][h-1].getSymbol() + "_"); 


            }
            System.out.printf("\n");
        } 
    }
}
class ABug {                 //ABug class
    int horpos, vertpos, energy, id;
    String species, name;
    Item symbol;

}

【问题讨论】:

  • 请打印您的堆栈跟踪
  • 错误:在类 buglife.main 中找不到主方法,请将主方法定义为:public static void main(String[] args) 或 JavaFX 应用程序类必须扩展 javafx.application.Application。 ?
  • 使用public static void main(String[] args) 定义您的主要方法。
  • 谢谢,线程“main”java.lang.Error 中的异常:未解决的编译问题:类型不匹配:无法从字符串转换为项目。我认为这是我需要更改项目或字符串的地方?

标签: java string


【解决方案1】:

这里的问题是ABug.symbolItem 枚举类型,而不是String。您需要一种在两者之间进行转换的方法。将此方法添加到Item

    public static Item findItemType(String symbol) {
        for(Item i : Item.values()) {
            if(String.valueOf(i.getSymbol()).equals(symbol)) {
                return i;
            }
        }
        return null;
    }

然后,更改您的 main() 方法以使用它:

            System.out.print("Please enter the symbol which you wish to represent the bug:");
            BugObj[i].symbol = Item.findItemType(reader.next());
            if(BugObj[i].symbol == null) {
                //Bad symbol!
            }

【讨论】:

  • 谢谢,非常感谢:)
猜你喜欢
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多