Java 项目/字符串获取输入时出现问题?

Java 项目/字符串获取输入时出现问题?,java,string,Java,String,对不起,如果我没有道理,我没有太多的练习 您好,我已将项目设置为保存一组字符,这些字符将放置在基于文本的映射上,但是当我请求字符时,出现一个错误,指出它必须是字符串类型。有没有办法将我的项目转换为字符串,或者允许输入项目?提前谢谢 用户试图输入符号时出错。我知道我可能会使用string.valueof,但是我不确定在哪里实现它 //imports import java.util.Scanner; import java.util.Random; public class main {

对不起,如果我没有道理,我没有太多的练习

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

用户试图输入符号时出错。我知道我可能会使用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;

}
//导入
导入java.util.Scanner;
导入java.util.Random;
公共班机{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
ABug[]BugObj=new ABug[4];//正在创建类ABug的对象BugObj
int循环=4;
int i=0;
整数圈;
MyClass worldnew=新的MyClass();
System.out.println(“请输入希望运行的循环数:”);
cycles=reader.nextInt();//获取要运行的周期数
System.out.print(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
worldnew.printWorld();//调用方法打印世界
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n\u F\uuuuuuuu-Food\n\u O\uuuuuuuuuuuuuuuuuuuuuu;
做{
BugObj[i]=new ABug();//正在创建实例
System.out.print(“请输入您希望表示错误的符号:”);
BugObj[i].symbol=reader.next();
System.out.print(“请输入错误名称:”);
BugObj[i].name=reader.next();
System.out.println(“请输入错误的种类:”);
BugObj[i].species=reader.next();
System.out.println(“请输入错误的水平位置:”);
BugObj[i].horpos=reader.nextInt();
System.out.println(“请输入错误的垂直位置:”);
BugObj[i].vertpos=reader.nextInt();
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuubug”+(+i+1)+“\uuuuuuuuuuuuuuuuuuuuuuuuuuu\n”);
System.out.println(“Symbol:+BugObj[i].Symbol);//打印错误信息
System.out.println(“名称:”+BugObj[i].Name);
系统.out.println(“物种:“+BugObj[i].物种”);
System.out.println(“水平位置:+BugObj[i].horpos”);
System.out.println(“垂直位置:+BugObj[i].vertpos+”\n\n”);
move(BugObj[i],worldnew);
i++;
System.out.println(“是否要输入另一个错误?\n 0-No,1-Yes\n”);
loop=reader.nextInt();
}while(loop==1);
}
公共静态无效移动(ABug错误,MyClass worldnew){
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“是否希望此错误移动?\n 0-No,1-Yes\n”);
if(reader.nextInt()==0)
{
系统出口(0);
}
int x=bug.horpos;
int y=bug.vertpos;
a项=bug.symbol;
setMap(x,y,bug.symbol);
worldnew.printWorld();
//获得bug的坐标
//集合映射[coor]=符号
//打印地图
//int r=(int)(Math.random()*(2--2))+-2;
//int originalHorpos=bug.horpos;
//int originalVertpos=bug.vertpos;
//bug.horpos=原始horpos+r;
//bug.vertpos=原始alvertpos+r;
//bug.horpos+=r;
//bug.vertpos+=r;
System.out.println(“新水平位置:+bug.horpos”);
System.out.println(“新垂直位置:+bug.vertpos”);
}
}
枚举项{
对象('O')、食物('F')、空间('');
私人最终字符符号;
项目(字符符号){
这个符号=符号;
}
char getSymbol(){return symbol;}
}
类MyClass{
公共无效集合映射(整数x,整数y,项目符号)
{
此.map[x][y]=符号;
}
项目[]地图=新项目[15][25];
公共世界(){
int v,h;//v-vert,h-hor

对于(v=1;v,这里的问题是
ABug.symbol
枚举类型,而不是
字符串
。您需要一种方法在两者之间进行转换。将此方法添加到

    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!
            }

请打印stacktraceError:Main方法未在buglife.Main类中找到,请将Main方法定义为:public static void Main(字符串[]args)或JavaFX应用程序类必须扩展JavaFX.application.application。?使用
public static void Main(字符串[]args)
定义主方法。谢谢,线程“Main”中出现异常java.lang.Error:未解决的编译问题:类型不匹配:无法从字符串转换为项。我想这就是我需要更改项或字符串的地方?谢谢,非常感谢:)