【问题标题】:Using While in java lab (User enters invalid input)在 Java 实验室中使用 While(用户输入无效输入)
【发布时间】:2012-04-11 02:02:59
【问题描述】:

我在课堂实验室的一部分方面遇到了一些问题,大部分都很容易,但由于某种原因,我的 while 语句没有按照我的意愿执行。它应该返回无效输入,然后提示用户租用;但是,它认为每个输入都是无效的。 (应该很容易在代码中看到)我该如何解决这个问题?或者我可以找到这些信息,因为我在我的书中找不到它。 (第二个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 ();
    }
}

【问题讨论】:

  • 请使用缩进,让您的代码更具可读性。
  • 很抱歉...在提交编辑之前我忘记实际检查我的更改。

标签: java while-loop


【解决方案1】:

你的逻辑错了。

你想要:

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

这与你写的相同。

DeMorgan's Law 是你的朋友 :)

注意,根据德摩根定律

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

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

当然,由于您使用整数作为有效选择,您也可以使用条件:

(choice < 0 || choice > 4)

【讨论】:

    【解决方案2】:

    你用过||在您的循环条件中。从逻辑上讲,您是在说:

    “如果选择不是0,或者不是1,或者不是2,等等”,意味着'1'的值满足条件,因为它不是0,也不是2。替换你的while循环:

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

    你会没事的。

    【讨论】:

      猜你喜欢
      • 2018-05-29
      • 2016-03-08
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      相关资源
      最近更新 更多