当通过基本的toString方法显示时,我的枚举类型没有从其默认的“null”值更改。当用户输入JPG,GIF等时,if语句应该识别输入的字符串对应于枚举。为什么if语句不接受用户的输入并将其转换为与枚举匹配?
public static void processPhotos()
{
String value;
String size;
String name;
String strType;
double dSize;
String photographer;
int iValue = 1;
Scanner kb = new Scanner(System.in);
while(iValue>0)
{
System.out.print("Enter the Photo's name.");
name = kb.nextLine();
System.out.print ("\nEnter the Photo's type(JPG, GIF, PNG, BMP, or OTHER).");
strType = kb.nextLine();
strType = strType.toUpperCase ( );
if(strType == "JPG")
{
type = Type.JPG;
}
if(strType == "GIF")
{
type = Type.GIF;
}
if(strType == "PNG")
{
type = Type.PNG;
}
if(strType == "BMP")
{
type = Type.BMP;
}
if(strType == "OTHER")
{
type = Type.OTHER;
}
System.out.print("\nEnter the Photo's size(IN Megabytes)");
size = kb.nextLine();
dSize = Double.parseDouble (size);
System.out.print("\nEnter the Photo's Photographer");
photographer = kb.nextLine();
Photo p = new Photo(name,type,dSize,photographer);
System.out.print (p.toString());
System.out.print("\n\nEnter an integer greater than zero to continue. Enter ZERO to end.");
value = kb.nextLine();
iValue = Integer.parseInt(value);
}
}
答案 0 :(得分:1)
将字符串与此方法进行比较。
if(strType.equals("JPG"))
然后比较两个字符串是否具有相同的值。
==比较两个字符串实际上是存储在内存中的相同对象,它们不是。
答案 1 :(得分:0)
if(strType == "JPG")
它应该是:
if(strType.equalsIgnoreCase( "JPG"))
因此,对于每个==
,将其更改为equalsIgnoreCase
或equals
答案 2 :(得分:0)
您必须使用equals而不是==
type == Type.JPG // comparing references
Type.JPG.equals(type) //Compares valus
答案 3 :(得分:0)
要比较字符串,您需要使用.equals
代替==
因此,您的代码应为if(strType.equals("JPG"))
public static void processPhotos()
{
String value;
String size;
String name;
String strType;
double dSize;
String photographer;
int iValue = 1;
Scanner kb = new Scanner(System.in);
while(iValue>0)
{
System.out.print("Enter the Photo's name.");
name = kb.nextLine();
System.out.print ("\nEnter the Photo's type(JPG, GIF, PNG, BMP, or OTHER).");
strType = kb.nextLine();
strType = strType.toUpperCase ( );
if(strType.equas("JPG"))
{
type = Type.JPG;
}
if(strType.equals("GIF")).
{
type = Type.GIF;
}
if(strType.equals("PNG"))
{
type = Type.PNG;
}
if(strType.equals("BMP"))
{
type = Type.BMP;
}
if(strType.equals("OTHER"))
{
type = Type.OTHER;
}
System.out.print("\nEnter the Photo's size(IN Megabytes)");
size = kb.nextLine();
dSize = Double.parseDouble (size);
System.out.print("\nEnter the Photo's Photographer");
photographer = kb.nextLine();
Photo p = new Photo(name,type,dSize,photographer);
System.out.print (p.toString());
System.out.print("\n\nEnter an integer greater than zero to continue. Enter ZERO to end.");
value = kb.nextLine();
iValue = Integer.parseInt(value);
}
}
答案 4 :(得分:0)
您不需要切换案例/条件检查,您可以将用户输入转换为枚举,如下所示:
enum Type {
JPG, GIF, PNG;
}
public static void main(String[] args) throws Exception {
String input = "GIF";
Type type = Type.valueOf(input);
System.out.println(type);
}
答案 5 :(得分:0)
你应该使用equals代替。这是一个讨论你为什么要这样做的主题。
简要地
== tests
用于参考相等。
.equals()
测试价值平等。
答案 6 :(得分:0)
在比较两个字符串时覆盖Object类的equals()方法。
ex:str1.equals(str2);
答案 7 :(得分:-1)
让您的生活更轻松并解析Enum。您不必编写那么多代码,如果枚举更改,您不必更改方法。如果strType不是枚举中的类型,您也可以选择默认的选择类型或执行其他操作。
if (Enum.TryParse<Type>(strType, out type))
type = Type.OTHER