I am a student, I have been writing this code in java to simulate a type of hand cricket game in java. The code is working but instead of batting once and then bowling it is asking me to do any one of the previous twice before giving the result.
I have tried all I could have. Is there any thing that I can do which is pretty simple and of my understanding . Or do I need to learn some more things before proceeding to continue with this project.
import java.util.*;
public class Main_Game {
public static String input() {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name: ");
String name=sc.nextLine();
return name;
}
public static void display(String name) {
System.out.println("\n\nHello "+name);
System.out.println("Welcome to the game !");
System.out.println("The rules of the game are very simple! \nyou enter a number less than 7 and computer enters another number.\nIf those numbers are same its an out depending on your choice of playing\n\n");
}
public static boolean toss() {
Scanner sc=new Scanner(System.in);
System.out.println("Enter what you choose Heads or Tails ");
String ch_toss=sc.nextLine();
int n;
if (ch_toss=="Tails") {
n=1;
} else {
n=0;
}
int rand=(int)(Math.random()*2)+0,flag=0;
if (n==rand) {
return true;
} else {
return false;
}
}
public static int batting() {
Scanner sc=new Scanner(System.in);
int wickets=0,runs=0;
while(wickets<4){
int bat=(int)(Math.random()*6)+1;
System.out.print("Enter your hit: ");
int bowl=sc.nextInt();
System.out.println("The computers throw: "+bat);
if(bowl<7){
if(bowl==bat){
System.out.println("Out !!!");
wickets++;
}
else{
runs+=bat;
}
}
}
return runs;
}
public static int bowling() {
Scanner sc=new Scanner(System.in);
int wickets=0,runs=0;
while(wickets<4){
int bowl=(int)(Math.random()*6)+1;
System.out.print("Enter your throw: ");
int bat=sc.nextInt();
System.out.println("The computers ball: "+bowl);
if(bat<7){
if(bowl==bat){
System.out.println("Out !!!");
wickets++;
}
else{
runs+=bat;
}
}
}
return runs;
}
public static int batting2() {
Scanner sc=new Scanner(System.in);
int wickets2=0,runs2=0;
while(wickets2<4){
int bat=(int)(Math.random()*6)+1;
System.out.print("Enter your hit: ");
int bowl=sc.nextInt();
System.out.println("The computers throw: "+bat);
if(bowl<7){
if(bowl==bat){
System.out.println("Out !!!");
wickets2++;
}
else{
runs2+=bat;
}
}
}
return runs2;
}
public static int bowling2() {
Scanner sc=new Scanner(System.in);
int wickets2=0,runs2=0;
while(wickets2<4){
int bowl=(int)(Math.random()*6)+1;
System.out.print("Enter your throw: ");
int bat=sc.nextInt();
System.out.println("The computers hit: "+bowl);
if(bat<7){
if(bowl==bat){
System.out.println("Out !!!");
wickets2++;
}
else{
runs2+=bat;
}
}
}
return runs2;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String won_toss_ch="";
String comp_ch="";
String won_toss_ch2="";
display(input());
int my_ch=0;
boolean res_toss=toss();
if(res_toss) {
System.out.println("You won the toss :)");
my_ch=1;
}
else {
my_ch=0;
int ch_comp=(int)(Math.random()*2)+0;
if (ch_comp==1) {
comp_ch="Batting";
}
else {
comp_ch="Bowling";
}
System.out.println("You lost the toss :( The computer chose "+comp_ch);
}
if (my_ch==1) {
System.out.println("What do you choose Bat or Ball ?");
won_toss_ch=sc.nextLine();
}
else {
if (comp_ch=="Batting") {
System.out.println("You are bowling");
won_toss_ch="Bowling";
won_toss_ch2="Batting";
}
else {
System.out.println("You are batting");
won_toss_ch="Batting";
won_toss_ch2="Bowling";
}
}
if (won_toss_ch=="Batting") {
System.out.println("\n\nYou made "+batting()+" runs");
}
else {
System.out.println("\n\nComputer made "+bowling()+" runs");
}
if (won_toss_ch2=="Batting") {
System.out.println("\n\nYou are Batting now");
System.out.println("\n\nYou made "+batting2()+" runs");
}
else if (won_toss_ch2=="Bowling") {
System.out.println("\n\nYou are Bowling now");
System.out.println("\n\nComputer made "+bowling2()+" runs");
}
if (batting2()>batting()) {
System.out.println("\n\nYou won");
}
else {
System.out.println("\n\nComputer won");
}
}
}
Aucun commentaire:
Enregistrer un commentaire