Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 需要帮助在我的代码中添加playreach循环吗_Loops_While Loop_User Input_Playback - Fatal编程技术网

Loops 需要帮助在我的代码中添加playreach循环吗

Loops 需要帮助在我的代码中添加playreach循环吗,loops,while-loop,user-input,playback,Loops,While Loop,User Input,Playback,这个程序假设从用户那里得到2个数字。不管是高是低,顺序都不重要。然后,它将导致计算机生成一个随机数,用户必须输入一个数字以查看是否匹配。然后,我的程序将显示用户编号是否过低或过高 我需要的帮助是在代码中加入playreach循环。它应该说“你想再玩一次吗?(是/否)”。然后它将获得用户字符串或字符,并将再次运行程序或结束程序(取决于用户输入的内容) 我已经工作和研究了好几个小时,似乎找不到一个办法(也许我很傻)。抱歉,如果我第二次发布这个问题,我需要在今天结束前完成 import java.ut

这个程序假设从用户那里得到2个数字。不管是高是低,顺序都不重要。然后,它将导致计算机生成一个随机数,用户必须输入一个数字以查看是否匹配。然后,我的程序将显示用户编号是否过低或过高

我需要的帮助是在代码中加入playreach循环。它应该说“你想再玩一次吗?(是/否)”。然后它将获得用户字符串或字符,并将再次运行程序或结束程序(取决于用户输入的内容)

我已经工作和研究了好几个小时,似乎找不到一个办法(也许我很傻)。抱歉,如果我第二次发布这个问题,我需要在今天结束前完成

import java.util.*;

/*
Guessing Game Group Project.  This game will ask for two numbers to set the range,
*/

public class Guess1
{
    public static void main(String [] args)
    {
        Scanner S = new Scanner(System.in);                                                                                                                             //Introduction Statement to game
        System.out.println("This is a guessing game.  I will ask you for two numbers.");
        System.out.println("I will choose a random number between your two numbers for you " + "\n" + "to try and guess.  As you guess, I will give you hints.");

        System.out.print("Choose two numbers to bound your range: ");                                                                                                   //Scanner to store user's input for the range
        int u1 = S.nextInt();
        int u2 = S.nextInt();
        int guess;
        int count = 0; // = 0 new
        String playAgain;

        int high;
        int low;
        if(u1 < u2)
        {
            high = u2;
            low = u1;
        }
        else
        {
            high = u1;
            low = u2;
        }
        Random gen = new Random();
        int rand = gen.nextInt(high-low+1) + low;
        System.out.println("Now guess a number between " + low + " and " + high + ": ");
        guess = S.nextInt();
        while(guess != rand)
        {
            count++;
            if (guess > high || guess < low)
            {
                System.out.println("Out of range. Please follow the directions dumb-ass.");
            }
            else if(guess > rand)
            {
                high = guess;
                System.out.println("Too high!");
            }
            else
            {
                low = guess;
                System.out.println("Too low!");
            }

            System.out.print("Now guess a number between " + low + " and " + high + ": ");
            guess = S.nextInt();

            if(guess == rand)
            {
                System.out.println("You got it!");
            }
            else if(count == 10)
            {
                System.out.println("You lost! So Sorry.");
            }
        }

    }
}
import java.util.*;
/*
猜谜游戏小组项目。这个游戏需要两个数字来设定范围,
*/
公开课猜测1
{
公共静态void main(字符串[]args)
{
扫描器S=新扫描器(System.in);//游戏简介
System.out.println(“这是一个猜谜游戏,我会问你两个数字。”);
System.out.println(“我将在您的两个数字中选择一个随机数,供您“+”\n“+”尝试猜测。在您猜测时,我将给您提示。”);
System.out.print(“选择两个数字来绑定您的范围:”;//扫描仪用于存储用户对范围的输入
int u1=S.nextInt();
int u2=S.nextInt();
智力猜测;
int count=0;//=0新
再次演奏弦乐;
int高;
int低;
if(u1高| |猜测<低)
{
System.out.println(“超出范围。请按照dumb-ass.的说明操作”);
}
else if(猜测>兰德)
{
高=猜测;
System.out.println(“太高了!”);
}
其他的
{
低=猜测;
System.out.println(“太低了!”);
}
System.out.print(“现在猜一个介于“+low+”和“+high+”:”之间的数字”;
猜测=S.nextInt();
如果(猜测=rand)
{
System.out.println(“明白了!”);
}
否则如果(计数=10)
{
System.out.println(“你输了!非常抱歉”);
}
}
}
}

您需要在游戏主体周围进行第二个循环,以询问继续问题。如果玩家说不,你可以退出。查看java doc for Scanner,查看它可以读入什么。

您需要在游戏主体周围进行第二个循环,以询问继续问题。如果玩家说不,你可以退出。查看java doc for Scanner,看看它能读入什么