TEL:400-8793-956
当前位置:程序、服务器

Java Arraylist循环问题

提问者: 近期获赞: 浏览人数: 发布时间:2021-01-14 13:39:34

 问:import java.util.InputMismatchException;

import java.util.Scanner;
import java.io.IOException;
import java.util.Random;
public class StringVariables {
    public static void main(String[] args) throws NumberFormatException,
            IOException {
        // user inputs their name in this section
        Scanner user_input = new Scanner(System.in);
//enter their first name
        
        String first_name;
        System.out.print("Enter Your First Name: ");
        while
            (!user_input.hasNext("[A-Za-z]+")) {
            System.out.println("Please only enter alphabet characters. Try again.");
            user_input.next();
        }
        first_name = user_input.next();
        
//enter their last name
        String last_name;
        System.out.print("Enter Your Last Name: ");
        while
            (!user_input.hasNext("[A-Za-z]+")) {
            System.out.println("Please only enter alphabet characters. Try again.");
            user_input.next();
        }
        last_name = user_input.next();
//full name printed together
        String full_name;
        full_name = first_name + " " + last_name;
        System.out.println(full_name + " Is Now Playing");
        // this is the shuffle portion as well as something to see if a number
        int numShuffles = -1;
        while (numShuffles < 0) {
            
            System.out.println("How many times do you want the numbers shuffled? ");
            try {
                numShuffles = user_input.nextInt();
            } catch (InputMismatchException inputException) {
                System.out.print("Please enter a valid number. \n");
                //this is the buffer that resets if the user types a letter instead of a number, or any other character
                user_input.next();
            }
        } 
        // here is going to be the loop for shuffles
        // we are now going to generate their random number and add a delay
        // after completing their name fields
        delay(3000);
        System.out
                .println(" You will be given " + numShuffles +  " hand(s) of 3 random numbers between 7-13" );
        delay(2000);
        System.out
                .println(" Then, the computer will add the random numbers and if it is equal to 31, you win!");
        /*
         * end of explanation of the game, next i will create a new screen with
         * the user's name and numbers
         */
        delay(4000);
        // printing 25 blank lines
        for (int i = 0; i < 25; i++)
            System.out.println(" ");
        
        System.out.println("User playing: " + full_name);
        
        System.out.println("Number of times shuffled: " + numShuffles);
        System.out.println("Your lucky numbers are...");
        // random number generator
        Arraylist numberStore = new Arraylist();
        Random random = new Random();
        while (true) {
            // the shuffle loop
            
            boolean isWinner = false;
            for (int i = 0; i < numShuffles; i++) {
                int num1 = 7 + random.nextInt(7);
                int num2 = 7 + random.nextInt(7);
                int num3 = 7 + random.nextInt(7);
                System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + (num1 + num2 + num3));
                numberStore.add(num1 + num2 + num3);
            int lastNumber = (numberStore.size() - 31);
            if (lastNumber == 31) {
                    isWinner = true;
                    System.out.println("Congratulations !! You are the Lucky Winner !!!!");
                    break;
                    //if you loose every shuffle
            }
            }
           if (!isWinner) {
                System.out.println("Better Luck Next Time");
           }
            // play again prompt
            System.out
                    .println(" Do you want to play again? (If you do enter y or yes) \n To exit press any other key ");
            String input = user_input.next();
            if (!"y".equalsIgnoreCase(input) && !"yes".equalsIgnoreCase(input)) {
                break;
            }
        }
        // if pressed y or yes the program will run again with the same number of shuffles entered from before
        user_input.close();
    }
    
    // delay field
    public static void delay(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException exp) {
            // delay field
        }
    }
}
你好!这是我对该网站的第一篇文章。我是Java新手,有一个问题。我有一个询问用户名称的程序,然后询问您要循环多少次(所以我的程序是它会生成3个介于7和13之间的随机数,如果加起来等于31,它们就是获胜者),我的问题是,我只希望最后打印出的数字在玩家赢或输的情况下计入,其他数字仅用于展示或挑逗。问题在于,无论玩家赢还是输,无论输赢,总是会输出失败的陈述。以上是我的整个代码。如果有人能帮上忙,那将是一种荣幸。
 
 
答:我已经解决了您的问题,请测试以下代码:
 
    导入java.util.ArrayList;
导入java.util.InputMismatchException;
导入java.util.Scanner;
导入java.io.IOException;
导入java.util.Random;
    公共类StringVariables {
        公共静态void main(String [] args)引发NumberFormatException,
                IOException {
            //用户在此部分输入他们的姓名
            扫描仪用户输入=新的扫描仪(System.in);
    //输入名字
 
            字符串名字;
            System.out.print(“输入您的名字:”);
            而
                (!userinput.hasNext(“ [A-Za-z] +”)){
                System.out.println(“请仅输入字母字符。再试一次。”);
                userinput.next();
            }
            名字= userinput.next();
 
    //输入姓氏
            字符串姓氏;
            System.out.print(“输入您的姓氏”);
            而
                (!userinput.hasNext(“ [A-Za-z] +”)){
                System.out.println(“请仅输入字母字符。再试一次。”);
                userinput.next();
            }
            姓= userinput.next();
    //全名打印在一起
            字符串全名;
            全名=名+“” +姓;
            System.out.println(全名+“正在播放”);
            //这是随机播放部分,也是看数字
            int numShuffles = -1;
            while(numShuffles <0){
 
                System.out.println(“您希望将数字重新排列多少次?”);
                尝试{
                    numShuffles = userinput.nextInt();
                } catch(InputMismatchException inputException){
                    System.out.print(“请输入一个有效的数字。\ n”);
                    //如果用户键入字母而不是数字或任何其他字符,这是重置的缓冲区
                    userinput.next();
                }
            } 
            //这将是洗牌的循环
            //现在我们将生成其随机数并添加一个延迟
            //完成其姓名字段后
            延迟(3000);
            系统输出
                    .println(“您将获得” + numShuffles +“ 7至13之间的3个随机数的手”);
            delay(2000);
            系统输出
                    .println(“然后,计算机将添加随机数,如果等于31,则您赢了!”);
            / *
             *游戏说明结束,接下来我将创建一个新屏幕
             *用户名和电话号码
             * /
            延迟(4000);
            //打印25个空白行
            对于(int i = 0; i <25; i ++)
                System.out.println(“”);
 
            System.out.println(“用户正在播放:” +全名);
 
            System.out.println(“洗牌次数:” + numShuffles);
            System.out.println(“您的幸运数字在...”);
            //随机数生成器
            ArrayList <Integer> numberStore =新的ArrayList <Integer>();
            随机random = new Random();
            而(true){
                //随机循环
 
                boolean isWinner = false;
                for(int i = 0; i <numShuffles; i ++){
                    int num1 = 7 + random.nextInt(7);
                    int num2 = 7 + random.nextInt(7);
                    int num3 = 7 + random.nextInt(7);
                     System.out.println(num1 +“ +” + num2 +“ +” + num3 +“ =” +(num1 + num2 + num3));
                    numberStore.add(num1 + num2 + num3);
 
                int lastNumber =(int)numberStore.get(numberStore.size()-1);               
                如果(lastNumber == 31){
                        isWinner = true;
                        System.out.println(“恭喜!您是幸运儿!!!!”);
                        打破;
                        //如果您松开所有混洗
                }
                }
               如果(!isWinner){
                    System.out.println(“下次好运”);
               }
                //再次播放提示
                系统输出
                        .println(“是否要再次播放?(如果输入y或yes)\ n要退出,请按任何其他键”);
                字符串输入= userinput.next();
                if(!“ y” .equalsIgnoreCase(input)&&!“ yes” .equalsIgnoreCase(input)){
                    打破;
                }
            }
            //如果按y或yes,则程序将再次运行,其运行次数与之前相同
            userinput.close();
        }
 
        //延迟字段
        公共静态无效延迟(int millis){
            尝试{
                Thread.sleep(millis);
            } catch(InterruptedException exp){
                //延迟字段
            }
        }
    }
 
 
答:我在第105行看到了问题
 
int lastNumber =(numberStore.size()-31);
 
使用int lastNumber =(int)numberStore.get(numberStore.size()-1);
上一篇: 如何在Android中分别显示openweather地图数据?
下一篇: 图像从数据库到jtable