我正在写一个问题,提示用户输入颜色(红色,蓝色,黄色,紫色和白色)&数量,将在以下输出中输出一个花:您有[数量] [花的类型]。每种颜色必须至少分配两朵鲜花。正如标题所述,我必须在程序中为变量创建一个switch语句,以初始化分配给该颜色的花的类型。以下是我写过的switch语句的示例:
public class Interests extends SQLiteOpenHelper {
private Context context;
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="userInterests";
private static final String TABLE_EXHIBITORS="exhibitors";
// Column Names //
private static final String KEY_ID="id";
public Interests(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
final String createQuery="CREATE TABLE IF NOT EXISTS "+TABLE_EXHIBITORS+"( "
+KEY_ID+" INTEGER PRIMARY KEY)";
db.execSQL(createQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_EXHIBITORS);
onCreate(db);
}
public void addExhibitor(int id){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_ID,id);
db.insert(TABLE_EXHIBITORS,null,values);
db.close();
Toast.makeText(context,"Exhibitor added",Toast.LENGTH_SHORT).show();
}
//This method is faulty//
public boolean getExhibitor(int id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.query(TABLE_EXHIBITORS,new String[]{KEY_ID},KEY_ID+" = ? ",new String[]{String.valueOf(id)},null,null,null);
c.close();
return (c!=null); //Always true
}
public void removeExhibitor(int id){
SQLiteDatabase db=this.getWritableDatabase();
db.delete(TABLE_EXHIBITORS,KEY_ID+" = ?",new String[]{String.valueOf(id)});
Toast.makeText(context,"Exhibitor removed",Toast.LENGTH_SHORT).show();
db.close();
}
}
我做错了什么?我觉得我必须包括一个可能的if语句。
答案 0 :(得分:1)
你看起来走在正确的轨道上。在您的案例陈述中,使用您在上面定义的flower
变量。为该颜色指定花的类型。此外,实际情况是一个字符串,因此它需要是双引号。
switch (color) {
case "red":
flower = "rose(s)";
break;
case "blue":
flower = "hydrangea(s)";
break;
}
然后,您可以在切换语句之后调用println
方法,如下所示:
System.out.println("You have " + quantity + " of " + flower);