我如何让这个 IF 语句起作用?

发布于 2024-12-07 11:30:01 字数 880 浏览 0 评论 0原文

修复了很多问题,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离笑几人歌 2024-12-14 11:30:01

在尝试对其执行数学运算之前,您可能希望将字符串解析为整数(在本例中小于或等于)。您可能希望尝试类似的操作:

import java.util.Scanner;

public class Salary {

double salary;
int sales;

public static void main(String[] args) {
   Scanner input = new Scanner ( System.in );
   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(" ");

   for(int i=0; i<salesArray.length; i++){
      Integer myInt = Integer.parse(salesArray[i]);
      System.out.print(myInt + " ");
      if (myInt <= 0) {
         System.out.println("Please enter a value greater than zero");}
      else {
         salary = (myInt * 0.09) + 200; }
      }
    }

}

您的解决方案检查字符串是否等于整数 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:

import java.util.Scanner;

public class Salary {

double salary;
int sales;

public static void main(String[] args) {
   Scanner input = new Scanner ( System.in );
   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(" ");

   for(int i=0; i<salesArray.length; i++){
      Integer myInt = Integer.parse(salesArray[i]);
      System.out.print(myInt + " ");
      if (myInt <= 0) {
         System.out.println("Please enter a value greater than zero");}
      else {
         salary = (myInt * 0.09) + 200; }
      }
    }

}

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".

单调的奢华 2024-12-14 11:30:01

salesArray 是字符串数组。 equals 方法应该接受一个字符串,即: salesArray[i].equals("0")

但正确的方法是使用 Integer.parseInt( ..)

The salesArray is array of strings. The equals method should take a string, that is: salesArray[i].equals("0")

But the proper way is to use Integer.parseInt(..)

人生戏 2024-12-14 11:30:01
if (Integer.parseInt(salesArray[i]) <= 0 ) {
if (Integer.parseInt(salesArray[i]) <= 0 ) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文