Java程序接受2个数字之间的输入

时间:2016-05-13 14:13:16

标签: java

这是我正在进行的一项简单的课堂练习。 (我对编程很新,所以如果这是一个简单的'菜鸟'错误,我为浪费你的时间道歉。)我不会撒谎:我发现很难知道 where 在编程时插入某些代码片段。

import java.util.*;


public class SuperSaveRandallTWyngaardC {

     static Scanner console=new Scanner(System.in);

    public static void main(String[] args) {

       char newCust;
       char promo;
       int itemNr=0;
       int qty=0;
       int price=0;
       int totalPrice=0;
       int custTot=0;
       int noOfItems=0;
       int grandTot=0;
       int custCount=0;

      System.out.println(" "); 
      System.out.println("*******SuperSave - your friendly local store.....*******");
      System.out.println(" ");
      System.out.print("New customer? (Y/N)>> ");
      newCust=console.next().charAt(0);
      newCust=Character.toUpperCase(newCust);

      while((newCust!='Y')&&(newCust!='N'))
     {
      System.out.print("Invalid option, please re-enter (Y/N)>> ");
      newCust=console.next().charAt(0);
      newCust=Character.toUpperCase(newCust);
     }
     if (newCust == 'N')
    {
     System.out.println("*******NO SALES THE WHOLE DAY.....*******");
    }
     else if (newCust == 'Y')
    {
     System.out.print("Please enter the item number (1000 -> 5000 or zero for none)>> ");
     itemNr=console.nextInt();  

        while ((itemNr<1000)&&(itemNr>5000)||(itemNr!=0))
       {
        System.out.print("Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> ");
        itemNr=console.nextInt();
       }
        if (itemNr==0)
        {
         System.out.println("*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******");
        }
         else if ((itemNr>1000)&&(itemNr<5000))
         {
          System.out.print("Enter quantity>> ");
          qty=console.nextInt();
         }
    }

    }    

}

运行程序。样本输出......

*******SuperSave - your friendly local store.....*******

New customer? (Y/N)>> y
Please enter the item number (1000 -> 5000 or zero for none)>> 1000
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 5000
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 999
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 5001
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 1234
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 4000
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 0
*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******

while循环表示任何项目编号输入无效(即使在1000-5000的指定范围内)

4 个答案:

答案 0 :(得分:2)

您的循环条件已关闭。

由于否定似乎混淆,首先写出好的东西:

(itemNr >= 1000 && itemNr <= 5000)  ||  itemNr == 0

即。必须介于1000和5000之间(含), OR 必须为0。

由于大多数人无法获得&& vs ||权限的优先权,因此您应该始终使用括号在混合时明确指定优先级,如我刚刚做了。

这样可以轻松地反转表达式,因为您只需反转所有内容,并保留括号:

(itemNr < 1000 || itemNr > 5000)  &&  itemNr != 0     // correct #1

将其与您所拥有的相比较,您将看到问题:

(itemNr < 1000) && (itemNr > 5000) || (itemNr != 0)   // wrong

由于> 5000表示肯定是!= 0,因此您可以像其他人一样重新安排表达式:

(itemNr < 1000 && itemNr != 0) || itemNr > 5000       // correct #2

从技术上讲,#2比#1表现更好,但这是你不会 注意到的差异。就个人而言,我发现#2不如#1那么直观,但这是一个意见问题。它们都让你想要你想要的。

答案 1 :(得分:1)

(itemNr<1000)&&(itemNr>5000)||(itemNr!=0)

没有itemNr使它成为现实。 考虑-1,1,1001,5001。

((itemNr<1000)&&(itemNr!=0))||(itemNr>5000)

你应该试试这个。

答案 2 :(得分:0)

(itemNr < 1000) && (itemNr > 5000)项目编号必须小于1000 AND 同时大于5000,这是不可能的。

您稍后会使用(itemNr > 1000) && (itemNr < 5000)进行类似的检查。

创建执行此检查的函数isInRange(int)会很有用。因此,您可以使用!isInRange(itemNr)(不在范围内)进行第一次检查。

不需要第二次检查,因为在while循环之后,数字将始终在范围(或0)内。

private static boolean isInRange(int i) {
    return ((i > 1000) && (i < 5000));
}

...

while(!isInRange(itemNr) && itemNr != 0) {
    ...
}

if (itemNr == 0) {
    ...
} else { // no need to check here
    ...
}

答案 3 :(得分:0)

要更正有效输入的检查,1000 ... 5000和0,请更新
while ((itemNr<1000)&&(itemNr>5000)||(itemNr!=0))

while ((itemNr < 1000 && itemNr != 0) || itemNr > 5000)

如果1000和5000是有效输入,那么您必须在获取数量时包括它们 else if ((itemNr>1000)&&(itemNr<5000))

else if ((itemNr >= 1000) && (itemNr <= 5000))

所以最后一个块看起来像

while ((itemNr < 1000 && itemNr != 0) || itemNr > 5000) {
    System.out.print("Invalid item nyumber, please re-enter (1000 -> 5000 or zero to stop)>> ");
    itemNr = console.nextInt();
}
if (itemNr == 0) {
    System.out.println("*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******");
} else if ((itemNr >= 1000) && (itemNr <= 5000)) {
    System.out.print("Enter quantity>> ");
    qty = console.nextInt();
}