0

我有点困惑,因为我无法弄清楚我在这里做错了什么。我已经做了我能做的几乎所有事情,但仍然无法正常工作。我现在不知道如何从这里前进。请提供帮助,我非常感谢您提供可能的解决方案。谢谢

public class Example {


public static void main(String[] args) {


    String cafescan;

    Scanner scan= new Scanner (System.in);   

       System.out.print ("Please enter the name of the cafe :" );
       cafescan = scan.nextLine();

       Cafe naam = new Cafe();
       naam.name = (cafescan);//THIS WORKS FINE 
       Cafe thau = new Cafe();
      thau.location = (location1);//THIS IS WHERE ERROR IS
              System.out.println (naam.setName());
          System.out.print   (thau.setLocation());

在我的 Cafe.jave 文件中,我有这个:

public class Cafe{
String name;
String location;



    public String setName(){

        name=name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase()+ " Cafe";
        return name;
    }   

    public String setLocation(){
        char location1 = name.charAt(0);
      //SWITCH IS SUPPOSED TO WORK ACCORDING TO FIRST ALPHABET OF cafescan

        switch (location1)
        {
        case 'E':
            System.out.println("Rosedale");
            break;
        case 'M':
            System.out.println ("Parkville");
            break;
        case 'T':
            System.out.println ("Towson");
            break;
            default:
                System.out.println("Baltimore");


        }
        return location;
    }

}

帮我看看大师:)

4

2 回答 2

1

首先你必须声明location1。他们从您的扫描仪中读取

String location1= scan.nextLine();
于 2013-04-15T06:43:19.553 回答
0

在您的主要方法中,使 Cafe 对象如下:

 Cafe naam = new Cafe(cafescan); 

如下修改您的咖啡馆课程:

public class Cafe{
   String name;
   String location;


public Cafe(String name){
    setName();
    setLocation();
}

public String setName(){

    name=name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase()+ " Cafe";
    return name;
}   

public String setLocation() {
    char location1 = name.charAt(0);
    // SWITCH IS SUPPOSED TO WORK ACCORDING TO FIRST ALPHABET OF cafescan

    switch (location1) {
        case 'E':
            System.out.println("Rosedale");
            location = "Rosedale";
            break;
        case 'M':
            System.out.println("Parkville");
            location = "Parkville";
            break;
        case 'T':
            System.out.println("Towson");
            location = "Towson";
            break;
        default: {
            System.out.println("Baltimore");
            location = "Baltimore";
        }

    }
    return location;
}

}

于 2013-04-16T11:12:55.383 回答