简体   繁体   English

在 java 实验室中使用 While(用户输入无效输入)

[英]Using While in java lab (User enters invalid input)

Im having some trouble with part of a lab for class, most of it is easy but for some reason my while statement is not doing what I want it to.我在 class 的部分实验室遇到了一些麻烦,其中大部分都很简单,但出于某种原因,我的 while 语句没有按照我的意愿进行。 It's supposed to return invalid input, then prompt user to renter;它应该返回无效输入,然后提示用户租用; however, it considers every input invalid.但是,它认为每个输入都是无效的。 (Should be easy to see in code) How do I fix this? (应该很容易在代码中看到)我该如何解决这个问题? Or were could I find the info because I cant find it in my book.或者我能找到这些信息吗,因为我在我的书中找不到它。 (The second while loop, first one works) (第二个 while 循环,第一个有效)

import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class lab8
{
    public static void main (String[] args)
    {
        int choice;
        String item;
        double costper;
        ArrayList <String> Items = new ArrayList <String> (); 
        Items.add ("computer");
        Items.add ("Monitor");
        Items.add ("Color Printer");
        Items.add ("Color Scanner");
        Items.add ("DVD/CDROM");
        Items.add ("To Quit");

        ArrayList <Integer> Num = new ArrayList <Integer> ();
        Num.add (0);
        Num.add (1);
        Num.add (2);
        Num.add (3);
        Num.add (4);
        Num.add (5);

        System.out.println ("\t\tMy Super Computer Store\n");
        int index=0;
        int endex= 0;
        while (index < Items.size() && endex < Num.size())
        {
            System.out.println ("\t" +Num.get(index)+"\t"+Items.get (endex));
            index++;
            endex++;
        }
        Scanner scan = new Scanner (System.in);
        System.out.print ("\n\t\t\tEnter the item to purchase: ");
        choice = scan.nextInt ();
        {
            if (choice==5)
            {
                JOptionPane.showMessageDialog (null, "Thanks for considering My Super Computer Store");
                System.exit (0);
            }
        }
        {
            if (choice==0 || choice==1 || choice==2 || choice==3 || choice==4)
            {
                item = Items.get (choice);
            }
        }
        while (choice!=0 || choice!=1 || choice!=2 || choice!=3 || choice!=4 || choice!=5)
        {
            System.out.println ("Invalid Input, Please enter a integer between 0 and 5. ");
            System.out.print ("\n\t\t\tEnter the item to purchase: ");
            choice = scan.nextInt ();    
        }
        System.out.print ("\n\n\t\t\tEnter the quantity to purchase: ");
        int Qty = scan.nextInt ();
    }
}

Your logic is wrong.你的逻辑是错误的。

You want:你要:

!(choice == 0 || choice == 1 || choice == 2 || choice == 3 || choice == 4)

which is not the same as what you wrote.这和你写的一样。

DeMorgan's Law is your friend:)德摩根定律是你的朋友:)

Note that by DeMorgan's Law请注意,根据德摩根定律

!(choice == 0 || choice == 1 || choice == 2 || choice == 3 || choice == 4)

is

(choice != 0 && choice != 1 && choice !=2 && choice !=3 && choice != 4)

And of course since you are using the integers you are for valid choices, you also could use the condition:当然,由于您使用的是有效选择的整数,因此您也可以使用以下条件:

(choice < 0 || choice > 4)

You've used ||你用过 || in your loop conditions.在你的循环条件下。 Logically, you're saying:从逻辑上讲,你是说:

"If the choice isn't 0, or isn't 1, or isn't 2, etc.", meaning that a value of '1' fulfills the conditions because it's not 0, and it's not 2. Replace your while loop with: “如果选择不是 0,或者不是 1,或者不是 2,等等。”,这意味着“1”的值满足条件,因为它不是 0,也不是 2。替换你的 while 循环和:

while (choice!=0 && choice!=1 && choice!=2 && choice!=3 && choice!=4)

And you'll be ok.你会没事的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM