java - Accept only unique input from a user into an ArrayList -
i @ loss of how allow user enter 3 unique numbers. have tried create array adds input , checks damage array make sure numbers unique, not seem work. thank help!!
arraylist<integer> damage = new arraylist<integer>(); arraylist<integer> unique = new arraylist<integer>(); (int k = 0; k<=10; k++) { unique.add(k); } { system.out.print("attack or defend? (a or d) "); option = keyboard.nextline().touppercase(); system.out.println(); switch (option) { case "a": system.out.println("enter 3 unique random numbers (1-10)"); for(int = 0; i<3; i++) { system.out.print("number " + (i+1) + ": "); input = keyboard.nextint(); if (input < 1 || input > 10) { system.out.println("error! enter valid number (1-10)"); } else { if (unique.contains(input)) { unique.remove(input); system.out.println(unique); damage.add(input); system.out.println(damage); i--; } else { unique.add(0, input); system.out.println("number not unique!"); } } } system.out.println(damage); system.out.println(); userdamage ahit = new userdamage(damage, option); name.getname(); ahit.setuserdamage(damage, option); system.out.println("\n"); cpuhealth-=ahit.getuserdamage(); cpu.setcpudamage(); userhealth-=cpu.getcpudamage(); system.out.println("\n\nhealth left: " + userhealth); system.out.println("computer health left: " + cpuhealth + "\n"); damage.clear(); option = null; break; default: system.out.println("invalid selection."); break; } } while(userhealth>0 || cpuhealth >0);
use contains
method java.util.list
determine if item present. javadoc:
boolean contains(object o)
returns true if list contains specified element. more formally, returns true if , if list contains @ least 1 element e such (o==null ? e==null : o.equals(e)).
Comments
Post a Comment