Your task is to recreate this game in Java, albeit in a simpler, text-based form
ID: 3783455 • Letter: Y
Question
Your task is to recreate this game in Java, albeit in a simpler, text-based format. The player enters sequences of three numbers to see if they follow the hidden rule. When the player has entered their sequence, the program should reply whether or not the sequence followed the rule.
How....or instruction
Create a loop which gives the player instructions on what to input. Then read input from the player. The input will be either one of two options: • If the user enters the word “answer” or some other string you choose to indicate the player is ready to end the game and guess. – In this case, prompt the user to guess our game’s rule, then output the answer. – Advanced We can use the .contains() method to analyze the guess and our output accordingly: If the guess contains the string “doubled” or “doubling” or “multiple,” the guess if most likely wrong. If the guess contains the string “increase” or “increasing,” the guess is most likely right and you can tailor the output accordingly. • Three numbers separated by spaces. – If the user enters a sequence that follows the rules, output “Yes!” – Otherwise output “No.”
Mehtod is gone be like the way....
There are a many ways to parse your input, but only a few that are easy. Here are a few different suggestions. • Only use Scanner.nextLine() to read in input. Using Scanner.nextInt() and Scanner.nextLine() together will lead to confusion. – String.split() is useful for breaking apart strings seperated by delimiters, such as commas, or, in this program, spaces. • How do I tell what the string the user input is? Check either if it’s the word “answer” or a sequence of numbers. • Regular expressions are also useful here, if you know how to use them.1 • Break up individual tasks into methods. For example you can have one method to grab user input, another method to detect whether a guess follows a rule, and so forth.
Explanation / Answer
import java.util.*;
public class guessgame {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int var;
gus g=new gus();
System.out.println("enter sentence:");
String num=sc.nextLine();
char ch[]=num.toCharArray();
System.out.println(ch.length);
if(ch.length<=4){
var=Integer.parseInt(num);
g.chkdigit(var);
}
else{
g.chkdigit(num);
}
}
}
import java.util.*;
public class gus {
static Scanner sc=new Scanner(System.in);
public static boolean chkdigit(int num){
int last=0;
int big=100;
boolean chk=false;
int reh=num;
while(reh>0){
last=reh%10;
if(last<big){
big=last;
chk=true;
}
else{
chk=false;
}
reh=reh/10;
}
if(chk==true){
System.out.println("the seqence is right:");
System.exit(0);
return true;
}
else
System.out.println("sequence is wrong:");
return false;
}
public static boolean chkdigit(String num){
int count=0;
int countsplit=0;
String guess="";
String temp="";
for(String retval: num.split(" ")) {
System.out.println(retval);
if((retval.equalsIgnoreCase("answer")))
count++;
}
if(count>0){
System.out.println("cguess the answer:");
guess=sc.nextLine();
String splitguess[]=guess.split(" ");
for(int i=0;i<splitguess.length;i++)
countsplit++;
String arr[]=new String[countsplit];
for(int i=0;i<splitguess.length;i++){
temp=splitguess[i];
if(i==0)
arr[0]=temp.concat("end");
if(i==1)
arr[1]=temp.concat("ing");
if(i==2)
arr[2]=temp.concat("ere");
}
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
return true;
}else
return false;
}
}output:
enter sentence:
123
3
the seqence is right:
enter sentence:
326
3
sequence is wrong:
enter sentence:
swa answer
10
swa
answer
cguess the answer:
things are
thingsend
areing
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.