布尔方法不断返回true

时间:2018-09-02 19:29:12

标签: java boolean

请帮助我理解为什么我编写的布尔方法未正确返回。我要求用户输入要存储在检查重复项的数组中的一系列整数。

public class DecisionTree extends AppCompatActivity {
    private static final String TAG = DecisionTree.class.getSimpleName();
    TextView txt,txt2;
    com.mycardboarddreams.autocompletebubbletext.MultiSelectEditText editText;
    String data;
    private ListView listView;
    private ItemAdapter itemListAdapter;
    private List<Item> itemList = new ArrayList<Item>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_decision_tree);
        /////////////////////////////////////////////////
        listView = (ListView) findViewById(R.id.diseases_list_container);

        itemListAdapter = new ItemAdapter(itemList, this);
        listView.setAdapter(itemListAdapter);
        listView.setFastScrollEnabled(true);

        ////////////////////////////////////////////////
        // add some items
        itemList.add(new Item("Jeans Refnum 2520"));
        itemList.add(new Item("T shirt Refnum 1220"));
            .
.
.
.
.
A long list of 100 items

        //add new items and changes to adapter
        itemListAdapter.notifyDataSetChanged();

        ////////////////////////////////////////////////
        //Text
        txt = (TextView) findViewById(R.id.textView);
        txt2 = (TextView) findViewById(R.id.textView2);
        txt.setText("Given a set of clinical features, this tool should provide you with a reasonable and relevant differential diagnsosis (not the definitive diagnosis!)");
        txt2.setText("Remember, this tool adds to your diagnostic skills and serves as an educational aid. It is not meant to replace your clinical judgement.");


       //the edittext
        editText = (com.mycardboarddreams.autocompletebubbletext.MultiSelectEditText) findViewById(R.id.auto_text_complete);

        //Add some sample items
        List<SampleItem> sampleItems = Arrays.asList(
                new SampleItem("Blue"),
                new SampleItem("Red"),
                new SampleItem("Orange"),
                new SampleItem("Yellow"),
                new SampleItem("vert"),
                new SampleItem("rouge")
        );

        editText.addAllItems(sampleItems);


        //Get the ListView associated with the MultiSelectEditText
        ListView list = editText.getListView();

        //Add it to a FrameLayout somewhere in the activity
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        list.setLayoutParams(params);


        final FrameLayout frame = (FrameLayout) findViewById(R.id.auto_list_container);
        frame.addView(list);



        // The output
        //frameLayout = (FrameLayout) findViewById(R.id.auto_list_container2);

        //Set a listener on bubble clicks
        //editText.setBubbleClickListener(new com.mycardboarddreams.autocompletebubbletext.MultiSelectEditText.BubbleClickListener<SampleItem>() {

           /* @Override
            public void onClick(SampleItem item) {
                //Log.d(TAG, "Item: " + item.getReadableName());
                Toast.makeText(DecisionTree.this, item.getReadableName(),
                        Toast.LENGTH_LONG).show();
                //displayListPatho();

            }
        });*/

           //-----------------------------------------END ONCREATE
    }

    public void blahblah (View view) {
        data= editText.getText().toString();
        listView.setVisibility(View.VISIBLE);
        Toast.makeText(DecisionTree.this, data,
                Toast.LENGTH_LONG).show();
    }

输出: $ java PokerHands

输入五张数字卡,不要输入面卡。使用2-9:

卡1:2

卡片2:3

卡片3:4

卡4:5

卡5:6

2、3、4、5、6,真

$ java PokerHands 输入五张数字卡,不要输入面卡。使用2-9:

卡1:2

卡2:2

卡片3:3

卡4:4

卡5:5

2,2,3,4,5,真

$ java PokerHands

输入五张数字卡,不要输入面卡。使用2-9:

卡1:2

卡2:2

卡片3:2

卡4:2

卡5:2

2,2,2,2,2,正确

3 个答案:

答案 0 :(得分:1)

这是因为第一次通过pprint-logical-blocki=0。因此它将始终返回true。您应该初始化j=0

答案 1 :(得分:1)

尝试将布尔值中的if更改为以下内容:

if (hand[i] == hand[j] && i != j)

答案 2 :(得分:0)

使用HashSet用法更改方法。 代码将是这样

private static boolean containsPair(int hand[])
{
    //adding to list all elements
    List<Integer> intList = new ArrayList<>();
    for (int i : hand) {
        intList.add(i);
    }
    //put all elements to set
    HashSet<Integer> zipcodeSet = new HashSet<>(intList);
    //if size not match , has a dublicate
    return zipcodeSet.size() != hand.length;

} // end containsPair()