Java基本高:低猜测游戏帮助(循环)

Java基本高:低猜测游戏帮助(循环),java,Java,我现在停止编程有一段时间了。大概4年左右,我只是想搞乱它,所以我决定做一个高:低数字猜测游戏。(猜一个数字1-100,程序说如果你的猜测太高或太低),我完全忘了我会怎么做: a) 一旦用户猜到正确的数字,询问他们是否想再次玩 b) 如果他们猜不到正确的数字(过高或过低),程序会让他们再次猜 我知道你需要循环,但我只是忘记了如何处理循环 package highlow; import java.util.Random; import java.util.Scanner; public clas

我现在停止编程有一段时间了。大概4年左右,我只是想搞乱它,所以我决定做一个高:低数字猜测游戏。(猜一个数字1-100,程序说如果你的猜测太高或太低),我完全忘了我会怎么做:

a) 一旦用户猜到正确的数字,询问他们是否想再次玩 b) 如果他们猜不到正确的数字(过高或过低),程序会让他们再次猜

我知道你需要循环,但我只是忘记了如何处理循环

package highlow;

import java.util.Random;
import java.util.Scanner;

public class guessing {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        Random rand = new Random();
        int tries;
        int correctNum = rand.nextInt(100);

        System.out.println("enter a number 1-100");
        int guess1 = input.nextInt();

        if(guess1 < correctNum){
            System.out.println("number is too low!");
        }
        else if(guess1 > correctNum){
            System.out.println("Number is too high!");
        }
        else if(guess1 == correctNum){
            System.out.println("correct!");
        }
        else{
            System.out.println("not a valid option");
        }


    }

}
package-highlow;
导入java.util.Random;
导入java.util.Scanner;
公共类猜测{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
Random rand=新的Random();
智力测验;
int correctNum=rand.nextInt(100);
System.out.println(“输入数字1-100”);
int guess1=input.nextInt();
如果(猜测1correctNum){
System.out.println(“数字太高了!”);
}
else if(猜测1==correctNum){
System.out.println(“正确!”);
}
否则{
System.out.println(“无效选项”);
}
}
}

您需要在
的同时将所有内容包装在一个
循环中,以便它不断重复,直到用户正确猜测:

// Make the scanner, get the random number etc... Put all the setup and
// stuff you don't want to be repeated here

while (true) {
    System.out.println("enter a number 0-99"); // Changed from 1-100 because rand.nextInt(100)
                                               // returns a number between 0 and 99
                                               // You can do correctNum += 1 to make it between 1 and 100
                                               // But put this in before the while loop starts
    int guess1 = input.nextInt();

    if(guess1 < correctNum){
        System.out.println("number is too low!");
    }
    else if(guess1 > correctNum){
        System.out.println("Number is too high!");
    }
    else if(guess1 == correctNum){
        System.out.println("correct!");
        break; // <---- Add this, this will make the loop stop when the 
               //player gets the answer correct and therefore the program will end
    }
    else{
        System.out.println("not a valid option");
    }
}
//制作扫描仪,获取随机数等。。。把所有的设置和
//你不想在这里重复的东西
while(true){
System.out.println(“输入数字0-99”);//由于rand.nextInt(100)而从1-100更改
//返回一个介于0和99之间的数字
//您可以执行correctNum+=1,使其介于1和100之间
//但是在while循环开始之前把它放进去
int guess1=input.nextInt();
如果(猜测1correctNum){
System.out.println(“数字太高了!”);
}
else if(猜测1==correctNum){
System.out.println(“正确!”);

break;//您需要将所有内容包装在
while
循环中,以便它不断重复,直到用户正确猜测:

// Make the scanner, get the random number etc... Put all the setup and
// stuff you don't want to be repeated here

while (true) {
    System.out.println("enter a number 0-99"); // Changed from 1-100 because rand.nextInt(100)
                                               // returns a number between 0 and 99
                                               // You can do correctNum += 1 to make it between 1 and 100
                                               // But put this in before the while loop starts
    int guess1 = input.nextInt();

    if(guess1 < correctNum){
        System.out.println("number is too low!");
    }
    else if(guess1 > correctNum){
        System.out.println("Number is too high!");
    }
    else if(guess1 == correctNum){
        System.out.println("correct!");
        break; // <---- Add this, this will make the loop stop when the 
               //player gets the answer correct and therefore the program will end
    }
    else{
        System.out.println("not a valid option");
    }
}
//制作扫描仪,获取随机数等…将所有设置和
//你不想在这里重复的东西
while(true){
System.out.println(“输入数字0-99”);//由于rand.nextInt(100)而从1-100更改
//返回一个介于0和99之间的数字
//您可以执行correctNum+=1,使其介于1和100之间
//但是在while循环开始之前把它放进去
int guess1=input.nextInt();
如果(猜测1correctNum){
System.out.println(“数字太高了!”);
}
else if(猜测1==correctNum){
System.out.println(“正确!”);
中断;//
包的高低;
导入java.util.*;
公共类猜测
{
公共静态void main(字符串[]args)
{
布尔wantstoplay=true;
while(wantstoplay)
{
play();
System.out.println(“您想再次播放吗?”);
扫描仪kb=新扫描仪(System.In);
如果((kb.nextLine().equals(“yes”)| |(kb.nextLine().equals(“yes”))
wantstoplay=true;
其他的
wantstoplay=假;
}
}
公共游戏
{
布尔播放=真;
int correctNum=(int)((Math.Random()*100)+1);
//从[1101]中选择随机双精度,然后向下舍入
int=0;
玩的时候
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入数字1-100”);
int guess=input.nextInt();
如果(猜测更正数量){
System.out.println(“数字太高了!”);
尝试++;
}
else if(猜测==correctNum){
System.out.println(“正确!”);
如果(尝试次数>1)
System.out.println(“恭喜,你猜对了数字。只花了你“+尝试+”尝试!”);
其他的
System.out.println(“你第一次就猜到了!干得好”);
}
否则{
System.out.println(“无效选项”);
}
}
}
}
上面是一些可能有用的示例代码。 我建议做一个play方法,然后在main方法中调用它。 这使您的代码更具组织性和可读性,因为现在您可以获得所需的功能,而无需使用一个包含两个循环的混乱方法

你会注意到我包含了while循环而不是for循环,这是因为当你不知道需要迭代多少次时,while循环是理想的

主方法中的while检查用户是否想要另一个游戏。请注意,它假定用户至少想要玩一个游戏。在进入循环之前,我通过将wantstoplay设置为true来完成此操作,但这也可以通过do while循环完成。有关更多信息,请参阅()

whilein-The-play方法检查用户是否需要进行另一次猜测,因为他还没有得到答案。就像我们不知道用户想在手前玩多少次一样,我们也不知道用户会进行多少次猜测

希望这能帮助你重返职业生涯