我如何让这个 IF 语句起作用?
修复了很多问题,在 myInt 声明期间出现错误,说找不到 parse 方法。我已经添加了其中提到的 NetBeans 导入,但仍然没有任何结果。这是更新后的代码:
import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;
public class Salary {
int salary;
int sales;
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
double Salary;
Salary salary = new Salary();
System.out.print( "Please enter the amount of items sold by the employees:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
Integer myInt = Integer.parse(salesArray[i]);
for(int i=0; i<salesArray.length; i++){
System.out.print(salesArray[i] + " ");
if (Integer.parseInt(salesArray[i]) <= 0) {
System.out.println("Please enter a value greater than zero");}
else {
Salary = (myInt * 0.09) + 200; }
}
}
}
非常感谢您的所有帮助,我真的很感激。
Fixed a lot of it, getting an error during the myInt declaration, saying it cannot find the parse method. I've added the imports NetBeans mentioned along with it but still nothing. Here's the updated code:
import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;
public class Salary {
int salary;
int sales;
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
double Salary;
Salary salary = new Salary();
System.out.print( "Please enter the amount of items sold by the employees:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
Integer myInt = Integer.parse(salesArray[i]);
for(int i=0; i<salesArray.length; i++){
System.out.print(salesArray[i] + " ");
if (Integer.parseInt(salesArray[i]) <= 0) {
System.out.println("Please enter a value greater than zero");}
else {
Salary = (myInt * 0.09) + 200; }
}
}
}
Thanks a lot for all the help, I really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在尝试对其执行数学运算之前,您可能希望将字符串解析为整数(在本例中小于或等于)。您可能希望尝试类似的操作:
您的解决方案检查字符串是否等于整数 0,但它永远不会等于,因为它是与整数进行比较的字符串。即使您检查了
salesArray[i].equals("0")
,这仍然只意味着它完全等于“0”,而忽略“000”或“0.0”等等效形式。您还在问题中指出您想要“小于或等于”关系,而不是“等于”关系。仅当字符串恰好为“0”时,进行字符串比较才为真。You will likely want to parse the string to an integer before trying to perform mathematical operations on it (less than or equal to, in this case). You may wish to try something like:
Your solution checked to see if the string was equal to the integer 0, which it never will be, since it is a string being compared to an integer. Even if you checked
salesArray[i].equals("0")
, this would still only mean it exactly equals "0", disregarding equivalent forms like "000" or "0.0". You also stated in your question that you wanted a "less than or equal to" relation, not an "equals" relation. Doing a string comparison will only be true if the string was exactly "0".salesArray
是字符串数组。equals
方法应该接受一个字符串,即:salesArray[i].equals("0")
但正确的方法是使用
Integer.parseInt( ..)
The
salesArray
is array of strings. Theequals
method should take a string, that is:salesArray[i].equals("0")
But the proper way is to use
Integer.parseInt(..)