班级项目 - 无法理解我正在做什么

时间:2015-10-04 17:42:38

标签: java

如果它含有麸质,可以在屏幕上显示“抱歉我不能吃”,否则它会显示“好吃”这样的东西

我添加了包含所有评论/说明的代码。我已经尝试了几个小时,但却无法弄清楚它为什么不起作用。

我在这里做错了什么?

import java.io.*;

public class JavaLab23{

    public static void main(String[] args) {
      // first we define our input streams.
      InputStreamReader input = new InputStreamReader(System.in);
      BufferedReader reader = new BufferedReader(input);   


            // put declarations here
      String peanutButter, chocolate, gluTen;  
      String cookieName ;
      String containsChocolate, containsPenutbutter;

      // we catch exceptions if some are thrown.
      try {
            //prompt the user for the animals name and read the input
            //the name will be used when prompting for more information
            System.out.println("what is the name of your cookie?");
            cookieName = reader.readLine();

            //prompt the user for the animals color and read the input
            System.out.println("Does  the " + cookieName + " cookie contain chocolate?");
            containsChocolate = reader.readLine();

            //comparing strings can be tricky, using a function 
            //such as compareTo, compareToIgnoreCase will be the best
            // 0 means the two strings are identical
            // <0 sFriendly precedes "yes", and >0 the other way around
           //if (containsChocolate.compareToIgnoreCase("yes")==0)
               System.out.println(" Does  the " + cookieName + " cookie contain peanut butter?");

               //if (containsChocolate.compareToIgnoreCase("yes")==0)
                //System.out.println("Does  the " + cookieName + " cookie contain peanut butter?");
                 //prompt the user for the animals color and read the input
            System.out.println("Does  the " + cookieName + " cookie contain gluten?");
            containsChocolate = reader.readLine();

            if (containsChocolate.compareToIgnoreCase("yes")==0)
                System.out.println(cookieName + "sorry i cant eat it.");


        /**************** TRUTH TABLE **************
          * condition 1 = peanutButter == yes
          * condition 2 = chocolate == yes
          * condition 3 = gluTen == no
          *              
          *  condition1    condition2   condition3      C1 or C2 and C3        (C1 OR C2) AND C3
          *  T             T            t                 T                     T
          *  T             T            F                 T                     F
          *  T             F            T                 T                     T
          *  T             F            F                 T                     F                  
          *  F             T            t                 T                     T
          *  F             T            F                 F                     F
          *  F             F            T                 F                     F
          *  F             F            F                 F                     F
          * 
          *  
          * ********************************************/

        System.out.println("C1: " + (peanutButter == yes) +" C2: "+ (chocolate == yes) + "C3 :" + (gluTen == no));
        System.out.println("C1 OR C2 AND C3" + (peanutButter == yes || chocolate == yes && gluTen == no);
        System.out.println("(C1 OR C2) AND C3" + ((peanutButter == yes || chocolate == yes) && gluTen == no));


      } catch (IOException e){
            System.out.println("Error reading from user");
       }

    }

}

2 个答案:

答案 0 :(得分:1)

你需要比较字符串(peanutButter.equals(&#34; yes&#34;))而不是(peanutButter == yes)。

答案 1 :(得分:0)

在比较两个String时,您应首先将常量String写为"yes".compareToIgnoreCase(peanutButter)==0,这样可以防止您出现NullPointerException。此外,对象无法与使用&#34; ==&#34;进行比较,String是一个java对象,你应该使用它的&#34; equals&#34;方法。请尝试以下代码。

if ("yes".compareToIgnoreCase(gluTen)==0)
    System.out.println(cookieName + " sorry i cant eat it.");
else
    System.out.println(cookieName + " okay to eat");



System.out.println("C1: " + ("yes".compareToIgnoreCase(peanutButter)==0) +" C2: "+ ("yes".compareToIgnoreCase(chocolate)==0) + "C3: " + ("no".compareToIgnoreCase(gluTen)==0));
System.out.println("C1 OR C2 AND C3" + ("yes".compareToIgnoreCase(peanutButter)==0 || "yes".compareToIgnoreCase(chocolate)==0 && "no".compareToIgnoreCase(gluTen)==0));
System.out.println("(C1 OR C2) AND C3" + (("yes".compareToIgnoreCase(peanutButter)==0 || "yes".compareToIgnoreCase(chocolate)==0) && "no".compareToIgnoreCase(gluTen)==0));