我正在尝试编写彩票程序(基于匈牙利游戏,从1到90的5个数字),效果很好,第一次给我错误的数字(大于90或0)时,它告诉我我做错了。但是第二次却没有。它继续使用无效数字执行游戏。你有什么秘诀,你们会做些什么?
谢谢您的时间。
代码:
import java.util.*;
class Lottery {
static int hits = 0;
Integer[] tippek = new Integer[5];
Random rand = new Random();
ArrayList<Integer> nums = new ArrayList<Integer>();
static Lottery lot = new Lottery();
public static void main (String[] args){
lot.wnums();
lot.tipread();
lot.tipcheck();
lot.wincheck();
lot.result();
}
public void wnums () {
// GENERATING THE WINNER NUMBERS
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; set.size() < 5; i++){
int x = rand.nextInt(90) + 1;
set.add(x);
}
nums.addAll(set);
}
public void tipread (){
// READING THE TIPS
System.out.println("Please write 5 different number from 1 to 90.");
try{
Scanner scan = new Scanner(System.in);
tippek[0] = scan.nextInt();
tippek[1] = scan.nextInt();
tippek[2] = scan.nextInt();
tippek[3] = scan.nextInt();
tippek[4] = scan.nextInt();
}catch (InputMismatchException ex){
System.out.println("Error.");
}
}
public void tipcheck() {
int fault = 0;
List<Integer> tips = Arrays.asList(tippek);
try{
for(int tipp : tippek){
System.out.println(tipp);
if(tipp == 0 || tipp > 90){
fault++;
}
}
if(fault == 1){
System.out.println("One of your tips is invalid ");
System.out.println("Write other numbers");
lot.tipread();
}
if(fault > 1){
System.out.println(fault + " of your tips are invalid ");
System.out.println("Write other numbers");
lot.tipread();
}
for(int tipp : tips){
for(int i = 0; i < tips.size(); i++){
if(tips.indexOf(tips.get(i)) != tips.lastIndexOf(tips.get(i))){
System.out.println("You can write a number only once");
lot.tipread();
}
}
}
}catch (NullPointerException ex){
System.out.println("Error.");
}
}
public void wincheck () {
// CHECKING THE TIPS
try{
for(int tipp : tippek){
for(int i = 0; i < 5; i++){
if(nums.get(i) == tipp){
hits++;
}
}
}
}catch(Exception ex){
System.out.println(" ");
}
}
public void result() {
try{
Arrays.sort(tippek);
Collections.sort(nums);
String tippeksor = Arrays.toString(tippek);
System.out.println("Your tips in ascending order: " + tippeksor);
System.out.println("You have " + hits + " hits.");
System.out.println("The winning numbers are: " + nums);
}catch(Exception ex){
lot.tipread();
}
}
}
答案 0 :(得分:1)
将OGSMayaCompoundNode*
包含在$rdx
中作为最后一个语句:
lot.tipcheck()
因此,确保每次用户输入有效数字时,都将对其进行检查。
其他修改:
从tipread()
中删除public void tipread (){
// READING THE TIPS
System.out.println("Please write 5 different number from 1 to 90.");
try{
Scanner scan = new Scanner(System.in);
tippek[0] = scan.nextInt();
tippek[1] = scan.nextInt();
tippek[2] = scan.nextInt();
tippek[3] = scan.nextInt();
tippek[4] = scan.nextInt();
lot.tipcheck();
}catch (InputMismatchException ex){
System.out.println("Error.");
}
}
在lot.tipcheck();
中的每个main()
之后添加lot.tipread();
答案 1 :(得分:1)
而不是直接从tipread
调用tipcheck
如果一切正常,tipcheck
返回true
,否则返回false
并在主目录中使用do-while
。
public static void main (String[] args){
lot.wnums();
do {
lot.tipread();
} while(!lot.tipcheck());
lot.wincheck();
lot.result();
}
和
public boolean tipcheck() {
int fault = 0;
List<Integer> tips = Arrays.asList(tippek);
try{
for(int tipp : tippek){
System.out.println(tipp);
if(tipp < 1 || tipp > 90){
fault++;
}
}
if(fault == 1){
System.out.println("One of your tips is invalid ");
System.out.println("Write other numbers");
// lot.tipread();
return false;
}
if(fault > 1){
System.out.println(fault + " of your tips are invalid ");
System.out.println("Write other numbers");
// lot.tipread();
return false;
}
for(int tipp : tips){
for(int i = 0; i < tips.size(); i++){
if(tips.indexOf(tips.get(i)) != tips.lastIndexOf(tips.get(i))){
System.out.println("You can write a number only once");
// lot.tipread();
return false;
}
}
}
}catch (NullPointerException ex){
System.out.println("Error.");
return false;
}
return true;
}
像这样