- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个按钮和一个文本字段,我想在单击按钮时在变量上添加值,除了我无法向字符串变量添加值外,一切正常
例如,如果我将值 20 放在 tempvalue 字符串上,它应该有 20,我把 30 放在它应该有 50,但我得到的是 null2050。
我尝试了 += 运算符,但没有用。
没有任何运算符可以在其上不断增加值(value),还是我必须编写新方法?
private String tempvalue;
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String getTxt = textField.getText();
tempvalue += getTxt;
System.out.println(tempvalue);
}
});
最佳答案
您从文本字段中获取字符串。
String input = getTxt;
您必须将字符串解析为整数或任何其他数字类型。
int value = Integer.parseInt(input);
然后就可以进行计算了。
您还应该经常检查用户输入是否真的是一个数字。使用 try/catch 避免输入错误:
int value = 0;
int firstValue = 5; //example variable
try{
value = Integer.parseInt(input);
}catch(Exception e1){
System.out.println("Your input could not be parsed to a number");
}
int result = firstValue + value; //always be sure all your values are numbers and not strings
System.out.println("Result: "+result);
总计:
private int tempvalue = 0;
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String getTxt = textField.getText();
int value = 0;
try{
value = Integer.parseInt(getTxt);
}catch(Exception e1){
System.out.println("Your input could not be parsed to a number");
}
tempvalue += value;
System.out.println("Result: "+tempvalue);
}
});
}
关于JAVA - 我如何保持增值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29279338/
value = a[0]; 在 Java 脚本中可以这样做吗? 最佳答案 没有。这是正确的: a[0] = value; 因为赋值运算符具有从右到左的结合性。 检查this link用于运算符优先级表
我有一个这样的哈希表: Hashtable ht = new Hashtable(); ht.put("A", "one"); ht.put("B", "two"); 然后我调用它的 values
当用户在输入框中键入内容时,我希望他们的名字显示在文本“hello”之后。与此相反,“hello”一词被删除并替换为他们的名字。 var name = document.getElementById(
我从这些地址开始 www.example.com/nav/Default.aspx www.example.com/nav/Fruit/Apple.aspx 我想要的是隐藏地址中的 nav 和 asp
是否可以将整数添加到作用域变量中? 例如 .... 如果顶部是 140,它应该给我 "280px" 而不是 "140140px" 这可能吗? 最佳答案 并不是我认为这是最好的方法,但您可以尝试: .
这是我的 fiddle : http://jsfiddle.net/JWww3/ 我不确定为什么这段代码不起作用。 当我单击“+1”按钮时,我想向“ajout”变量添加+1。如果 ajout = 5(
我正在尝试更新/创建一个新的数据集并将先前的值与新的值相结合。 这就是我的 Python 脚本现在的样子。 dailyDataset = { "pId" : pub, "oId" :
这是我的 Firebase 键值图像 这是我的代码: Query query = myFirebaseRef.orderByChild("age"); query.addListene
我是一名优秀的程序员,十分优秀!