我有点困惑,因为我无法弄清楚我在做什么错。我已尽我所能,但仍无法正常工作。我现在不知道如何从这里前进。请帮助,我真的非常感谢您可能的解决方案。谢谢
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;
}
}
帮我看看大师:)
最佳答案
首先,您必须声明location1。他们从您的扫描仪读取
String location1= scan.nextLine();