我正在为一个java程序构建一个登录系统,该程序将计算某些商店的员工工资。 在我进一步发展之前,我完全清楚这不是最安全的登录方式,所以请不要尝试解释,因为我知道。 到目前为止我的代码低于
public class project15
{
public static void main(String[] args);
{
String store = store501;
String stroePin = 1468dty;
String personalPin = abc123;
String inputStore;
String inputPin;
String inputPersonal
inputStore = JOptionPane.showInputDialog("please enter the store number");
inputPin = JOptionPane.showInputDialog("please enter the stores unique ID");
inputPersonal = JOptionPane.showInputDialog("please enter your persoanl ID");
if (inputStore==store && inputPin==stroePin && inputPersonal==personalPin){
System.out.println("correct information");
}
else
{
System.out.println("incorrect information");
}
}
}
我在前三个String值上出现错误,例如,“store501无法解析为变量。”其他所有内容似乎都正常工作。任何人都可以找到我出错的地方并请解释,因为我渴望从我可能犯过的任何错误中吸取教训。在此先感谢,我感谢任何反馈。
答案 0 :(得分:1)
似乎store501
,1468dty
和abc123
是未定义的变量。
您必须在使用前定义它们。类似的东西:
String store501 = "some string";
或许你希望他们成为Strings
。在那种情况下:
String store = "store501"; // Strings are between ""
详细了解Java docs或Java tutorials中的Strings
。
<强>记住:强>
比较String
使用:
str1.equals(str2)
不
str1 == str2
答案 1 :(得分:1)
您的计划中存在多个错误。首先,关于声明String常量,其他人都说了些什么。但那也是另一个错误。
您正在使用==来比较字符串。 Java表达式a == b比较两个变量,如果两个变量都引用相同的Object,则返回true。它不比较对象以查看它们是否具有相同的值。
您的程序可以设置personalPin =“abc123”;当提示输入input时,用户可以键入abc123,但personalPin和inputPersonal可能会引用两个碰巧具有相同值的不同String对象。也就是说,即使两个字符串都是“abc123”,personalPin == inputPersonal也可能是假的。
修复它的方法是使用String.equals()函数来比较字符串的内容:
personalPin.equals(inputPersonal)
答案 2 :(得分:0)
创建字符串时,需要用“”
括起来String store = "store501";
String stroePin = "1468dty";
String personalPin = "abc123";
String inputStore;
String inputPin;
String inputPersonal
inputStore = JOptionPane.showInputDialog("please enter the store number");
inputPin = JOptionPane.showInputDialog("please enter the stores unique ID");
inputPersonal = JOptionPane.showInputDialog("please enter your persoanl ID");
if (store.equals(inputStore) && stroePin.equals(inputPin) && personalPin.equals(inputPersonal)){
System.out.println("correct information");
} else {
System.out.println("incorrect information");
}
答案 3 :(得分:0)
将您的代码更改为:
String store = "store501";
String stroePin = "1468dty";
String personalPin = "abc123";
答案 4 :(得分:0)
如果它们是字符串变量,那么你应该将它们放在“”中。你可以这样做:
String store = "store501";
String stroePin = "1468dty";
String personalPin = "abc123";
OR:
String store;
String stroePin;
String personalPin;
store="store501";
stroePin="1468dty";
personalPin="abc123";