Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

Incompatible Type - Guessing Game

$
0
0

Hi, please help me, when I try to run my code for the Guessing Game, I get an: incompatiable type compiler error. What's happening?

import java.util.; import java.io.; public class Main { private String answer; private String symbol; private int digits; private int max; private int min; private int limit; private int lieCount; private int compareNumber; private List<integer> answerOutput = new ArrayList<integer>();

public static void main(String[] guessingGame)
throws java.lang.Exception
{ 
    Main guessing = new Main(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    Scanner entry = new Scanner(System.in);
    int testCases = entry.nextInt(); 
  // int testCases = Integer.parseInt(reader.readLine());

    for (int i = 1; i <= testCases; i++)
    {
        List<String> hintList = new ArrayList<String>();
        guessing.min = -100;
        guessing.max = -100;
        guessing.compareNumber = -100;

         int hints = entry.nextInt(); 
       // int hints = Integer.parseInt(reader.readLine());
        // totalHints += hints; 
        for (int j = 1; j <= hints; j++)
        {
            hintList.add(reader.readLine()); 
        }

        guessing.evaluate(hintList);

    }

    for (int element : guessing.answerOutput)
    {
        System.out.println(element); 
    }
}

public void evaluate (List<String> list)
{
    switchCase(list.get(0)); // this method will extract the math symbol, the number, and the answer
    getLimit(this.symbol, this.digits, this.answer); // get the max and min boundaries of first element
    this.lieCount = 0; 
    for (int i = 1; i < list.size(); i++)
    {
        switchCase(list.get(i));
        comparePreviousHints(this.symbol, this.digits, this.answer); 
    }

   answerOutput.add(lieCount); 
}


public void switchCase(String element)
{
    List<String> numbers = new ArrayList<String>();
    StringBuilder sbNum = new StringBuilder(); 
    StringBuilder sbSymbol = new StringBuilder(); 
    StringBuilder sbAnswer = new StringBuilder(); 
    for (int i = 0; i < element.length(); i++)
    {
        char letter = element.charAt(i); 
        if (Character.isLetter(letter))
        {
            sbAnswer.append(""+letter); 
        }
        if (Character.isDigit(letter))
        {
            sbNum.append(""+letter);
        }
        if (Character.getType(letter) == Character.MATH_SYMBOL)
        {
             sbSymbol.append("" + letter); 
        }
    }
      //this.symbol = element.charAt(0)+""; 
      this.symbol = sbSymbol.toString(); 
      this.digits = Integer.parseInt(sbNum.toString()); 
      this.answer = sbAnswer.toString();

    //String sym = element.charAt(0)+""; 
    //System.out.println(sbNum.toString()); 
    //System.out.println(sym); 
    //System.out.println(sbAnswer.toString()); 
}


public void getLimit(String symbol, int digits, String answer)
{
    switch(symbol)
    {
        case "<":
        if (answer.equalsIgnoreCase("no"))
        {
            // example num < 100; 
            this.min = digits; 
            //this.max = Integer.MAX_VALUE; 
        }
        else
        {
            this.max = digits;
           // System.out.println("max = " + max);
            //this.min = Integer.MIN_VALUE; 
        }
        break;

        case ">":
        if (answer.equalsIgnoreCase("no"))
        {
            this.max = digits;
            //this.min = Integer.MIN_VALUE; 
        }
        else
        {
            this.min = digits; 
            //this.max = Integer.MAX_VALUE; 
        }
        break;

        case "=":
        this.compareNumber = digits;
        break;

    }

    //this.limit = (this.min > this.max)? min : max; 
    //return limit;

}

public void comparePreviousHints(String sym, int num, String ans)
{
    // is this new number smaller or bigger than our max?
    // if smaller, than that's okay, if bigger, than not okay. 
  //  System.out.println("lie count = " + lieCount);

     switch(sym)
    {
        case "<":
        if (ans.equalsIgnoreCase("yes") && this.max == -100)
        {
              this.max = num;   
        }

        if (ans.equalsIgnoreCase("no"))
        {
            if (this.max != -100 && num > this.max)
            {
                this.lieCount++; 
            }
        }

        else if (ans.equalsIgnoreCase("yes") && num < this.min)
        {
                lieCount++; 
        }

        else if (ans.equalsIgnoreCase("yes") && this.min == 0)
        {
            lieCount++; 
        }
        break;

        case ">":
        // System.out.println("num = " + num); 
        // System.out.println("max - num = " + (max-num)); 
        // System.out.println("answer = " + ans);

        if (ans.equalsIgnoreCase("yes") && this.min == -100)
        {
              this.min = num;   
              //System.out.println("min = " + min);  
        }

        if (ans.equalsIgnoreCase("no") && num < this.min)
        {
            lieCount++; 
        }

        if (ans.equalsIgnoreCase("yes"))
        {
            if (this.max != -100  && num > this.max)
            {
                lieCount++; 
            }
            else if (this.max - num == 1)
            {
               lieCount++; 
            }
            else if (this.max == Math.pow(10,9))
            {
                  lieCount++; 
            }

        }
        break;

        case "=":
        if (this.compareNumber == -100)
        {
            this.compareNumber = num;
           // System.out.println("compare number = " + compareNumber); 
        }
        else if (num != compareNumber)
        {
         //   System.out.println("num != compareNumber");
            lieCount++; 
        }
        break;


    }
}

}


Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>