Java 更新-错误消息:";无法打开字符串[]类型的值。仅允许使用可转换的int值、字符串或枚举变量;
我现在已经按照建议将开关(选项)更改为开关(输入),并且删除了之前收到的错误消息。但是程序并没有按它应该的方式运行 它创建了一个带有下拉菜单的窗格,将1-5列为选项,但当我选择选项1、2或4时,它什么也不做。它应该显示来自相应方法的信息。我不知道如何解决这个问题 当我选择选项3时,它会按应有的方式显示输入框,但无论我输入什么,它都会给出相同的结果。我相信这是因为它们被初始化为0,但我不确定如何在没有数字的情况下初始化它 另外,当我选择选项5时,它应该退出程序,但它没有退出,我也不知道如何解决这个问题 有什么建议吗 方法:Java 更新-错误消息:";无法打开字符串[]类型的值。仅允许使用可转换的int值、字符串或枚举变量;,java,user-interface,switch-statement,joptionpane,Java,User Interface,Switch Statement,Joptionpane,我现在已经按照建议将开关(选项)更改为开关(输入),并且删除了之前收到的错误消息。但是程序并没有按它应该的方式运行 它创建了一个带有下拉菜单的窗格,将1-5列为选项,但当我选择选项1、2或4时,它什么也不做。它应该显示来自相应方法的信息。我不知道如何解决这个问题 当我选择选项3时,它会按应有的方式显示输入框,但无论我输入什么,它都会给出相同的结果。我相信这是因为它们被初始化为0,但我不确定如何在没有数字的情况下初始化它 另外,当我选择选项5时,它应该退出程序,但它没有退出,我也不知道如何解决这个
import java.util.Properties;
public class System_Y3881268
{
// the attributes
private String make;
private String model;
private int speed;
private int memorySize;
private double hardDiskSize;
private double purchaseCost;
// the methods
// the constructor
public System_Y3881268(String makeIn, String modelIn, int speedIn)
{
make = makeIn;
model = modelIn;
speed = 2;
}
// methods to set the corresponding attributes- mutator methods
public void setMemory(int memorySizeIn)
{
memorySize = memorySizeIn;
}
public void setHardDisk(double hardDiskSizeIn)
{
hardDiskSize = hardDiskSizeIn;
}
public void setPurchaseCost(double purchaseCostIn)
{
purchaseCost = purchaseCostIn;
}
// methods to return attribute values- accessor methods
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public int getProcessorSpeed()
{
return speed;
}
//display details of the system
public void displayDetails()
{
System.out.println("*****Computer Details*****");
System.out.println("Make: " + getMake());
System.out.println("Model: " + getModel());
System.out.println("Processor speed; " + getProcessorSpeed() + "GHz");
System.out.println("Memory size: " + memorySize + " MB");
System.out.println("Hard disk size: " + hardDiskSize + " GB");
System.out.println("Purchase cost: £" + purchaseCost);
}
//check if the hard disk size is below 2GB and print corresponding message
public String checkHDStatus(double hardDiskSizeIn)
{
if(hardDiskSizeIn<2)
{
return("Low");
}
else
{
return("Ok");
}
}
//Check if the memory size is below 128MB and print corresponding message
public boolean goodMemorySize(int memorySizeIn)
{
if(memorySizeIn<128)
{
return false;
}
else
{
return true;
}
}
/*use the checkHDStatus() method and the goodMemorySize() method to diagnose the
* system by displaying the appropriate messages*/
public void diagnoseSystem()
{
System.out.println();
System.out.println("*****System Diagnosis*****");
System.out.println("Hard disk size = " + checkHDStatus(hardDiskSize));
System.out.println("Memory size Ok = " + goodMemorySize(memorySize));
}
//method to display system properties
public static void displaySystemProperties()
{
Properties pros = System.getProperties();
pros.get(System.out);
System.out.println();
System.out.println("*****System Properties*****");
System.out.println("Operating System Architecture: " + System.getProperty("os.arch"));
System.out.println("Operating System Name: " + System.getProperty("os.name"));
System.out.println("Operating System Version: " + System.getProperty("os.version"));
System.out.println("User Account Name: " + System.getProperty("user.name"));
System.out.println("Java Version: " + System.getProperty("java.version"));
System.out.println();
if(System.getProperty("os.name").equals("Windows 10"))
{
System.out.println("Thumbs up! Your operating system is Windows 10");
}
else if(System.getProperty("os.name").equals("Linux"))
{
System.out.println("Thumbs down! Your operating system is Linux");
}
else
{
System.out.println("Your choice of operating system is ok");
}
}
}
import java.util.Properties;
公共类系统_Y3881268
{
//属性
私人字符串制作;
私有字符串模型;
私人整数速度;
私有内存化;
私人双硬盘大小;
私人双重购买成本;
//方法
//构造器
公共系统_Y3881268(字符串生成、字符串建模、整数速度输入)
{
make=makeIn;
model=modelIn;
速度=2;
}
//方法来设置相应的属性-mutator方法
公共void setMemory(int memorySizeIn)
{
memorySize=memorySizeIn;
}
公共无效设置硬盘(双硬盘大小)
{
硬盘大小=硬盘大小;
}
public void setPurchaseCost(双purchaseCostIn)
{
purchaseCost=purchaseCostIn;
}
//返回属性值的方法-访问器方法
公共字符串getMake()
{
退货;
}
公共字符串getModel()
{
收益模型;
}
public int getProcessorSpeed()
{
返回速度;
}
//显示系统的详细信息
公开资料
{
System.out.println(“******计算机详细信息*****”);
System.out.println(“Make:+getMake());
System.out.println(“Model:+getModel());
System.out.println(“处理器速度;”+getProcessorSpeed()+GHz”);
System.out.println(“内存大小:+memorySize+MB”);
System.out.println(“硬盘大小:+硬盘大小+”GB”);
系统输出打印项次(“采购成本:£”+采购成本);
}
//检查硬盘大小是否低于2GB,并打印相应消息
公共字符串checkHDStatus(双硬盘大小)
{
如果在开关(输入)
中(硬盘大小)更改开关(选项)
,则需要进行一些修复,但是,由于没有显示整个代码,因此无法完成
第一个问题是试图切换字符串数组而不是字符串
这:
可以重写为:
String[] choices = { "Select...", "1", "2", "3", "4", "5" };
String input = (String) JOptionPane.showInputDialog(null, "Select a choice\n1: Print System Details\n2: Diagnose System\n3: Set Details\n4: Print System Properties\n5: Quit the Program", "Computer System Menu", JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);
switch (input) { // switch on the value you want to check, the one you entered yourself
case "1": s.displayDetails(); // there is no need for { and } here, it just adds lines in your code
break;
现在,这将解决您原来的问题
下一个问题是在while循环中:
while (choice != '5');
选择永远不会是“5”,因为您永远不会更新选择的值。
一种选择是将其更改为:
while (!"5".equals(input));
因为您已经有了输入
变量,并且还可以进行比较
正如我所说的,您没有显示整个代码,因此很难检查所有内容。您尝试在字符串数组上执行切换,而您可以在单个字符串上切换,而不是在数组上切换。将此更改为:switch(choices)改为switch(input)谢谢。错误已经消失。但是程序仍然无法正常运行。新的错误/问题是什么?在您更新后,很抱歉,我们无法帮助您,因为您没有显示您在这些方法中所做的操作。您可能需要进一步澄清原因。您的“答案”已经出现在对原始问题的评论中。
while (choice != '5');
while (!"5".equals(input));