Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class HW13_1 { //Desc: The computer randomly se

ID: 3622994 • Letter: I

Question

import java.util.Scanner;
public class HW13_1
{
//Desc: The computer randomly selects a number within the range and the player guesses
// what the number is. The computer will give clues to help the player. However,
// after each guess, the computer can increase the number by one, decrease the
// number by one, or stay the same. The game will continue until the user guesses
// the correct number. The user has the option to play again after each game.
//Input: The user gives number.
//Output: The computer displays various messages throughout the game to help it run.
public static void main(String[] args)
{
int[][] gamesummary = new int[30][30];
Scanner input = new Scanner(System.in);
while (true)
{
int change = 0, guess = 0;
soP("What is the lowest allowed number: ",0);
int min = input.nextInt();
soP("What is the highest allowed number: ",0);
int max = input.nextInt();
int number = (int)(Math.random()*(max-min+1))+min;
boolean flag = false;
soP("Guess a number in the range ["+min+".."+max+"]: ",0);
do
{
guess = input.nextInt();
if (flag == false)
number+=change;
if (guess > number)
soP("Too high: ",0);
else if (guess < number)
soP("Too low: ",0);
else
{
soP("You got it! ",1);
flag = true;
}
if ((number == max)&&(flag == false))
change = extremE(change, 0);
else if ((number == min)&&(flag == false))
change = extremE(change, 1);
else if(flag == false)
change = extremE(change, 2);
}
while (guess != number);
soP("Input 2 to Exit (or you will have to play again!): ",0);
int quit = input.nextInt();
if (quit==2)break;
}
}
public static void soP(String s, int i)
{
if (i==0)
System.out.print(s);
else if (i==1)
System.out.println(s);
else return;
}
public static int extremE(int change, int i)
{
if (i==0)
{
change = -1*((int)(2*Math.random()));
return change;
}
else if (i==1)
{
change = (int)(2*Math.random());
return change;
}
else if (i==2)
{
change = (int)(3*Math.random())-1;
return change;
}
return 0;
}
}

Explanation / Answer

import java.util.Scanner;
public class HW13_1
{
//Desc: The computer randomly selects a number within the range and the player guesses
// what the number is. The computer will give clues to help the player. However,
// after each guess, the computer can increase the number by one, decrease the
// number by one, or stay the same. The game will continue until the user guesses
// the correct number. The user has the option to play again after each game.
//Input: The user gives number.
//Output: The computer displays various messages throughout the game to help it run.
    public static void main(String[] args)
    {
        int[][] gamesummary = new int[30][30];
        Scanner input = new Scanner(System.in);
        while (true)
        {
            int change = 0, guess = 0;
            soP("What is the lowest allowed number: ",0);
            int min = input.nextInt();
            soP("What is the highest allowed number: ",0);
            int max = input.nextInt();
            int number = (int)(Math.random()*(max-min+1))+min;
            boolean flag = false;
            soP("Guess a number in the range ["+min+".."+max+"]: ",0);
            do
            {
                guess = input.nextInt();
                if (flag == false)
                number+=change;
                if (guess > number)
                soP("Too high: ",0);
                else if (guess < number)
                soP("Too low: ",0);
                else
                {
                soP("You got it! ",1);
                flag = true;
                }
                if ((number == max)&&(flag == false))
                change = extremE(change, 0);
                else if ((number == min)&&(flag == false))
                change = extremE(change, 1);
                else if(flag == false)
                change = extremE(change, 2);
                if(change==1)
                    System.out.println("Number is incremented by 1");
                else if(change==-1)
                    System.out.println("Number is decremented by 1");
                else
                    System.out.println("No change in the number");
                soP("Guess a number in the range ["+min+".."+max+"]: ",0);
            }
            while (guess != number);
            soP("Input 2 to Exit (or you will have to play again!): ",0);
            int quit = input.nextInt();
            if (quit==2)break;
        }
    }
    public static void soP(String s, int i)
    {
        if (i==0)
        System.out.println(s);
        else if (i==1)
        System.out.println(s);
        else return;
    }
    public static int extremE(int change, int i)
    {
        if (i==0)
        {
        change = -1*((int)(2*Math.random()));
        return change;
        }
        else if (i==1)
        {
        change = (int)(2*Math.random());
        return change;
        }
        else if (i==2)
        {
        change = (int)(3*Math.random())-1;
        return change;
        }
        return 0;
    }
}