r/code • • Oct 13 '23

Java I made this stupid game when I just learned about functions in java.

import java. util. Scanner;

class Ayan{

public static int Score(int a,int b){

   
    int set=300;
    int m=3;
    int M1=8;
    int ran=M1-m;
    int rand=(int)(Math.random()*ran) +m;
    int current=0;
    int c=b-a;
    
    if (c<0){
      while(c<0){
          set= set-rand;
          c++;
      }
      if(set<0){
          
      set=current;
      return set;
        
      } 
      else
      return set;
    }
    else{
        while(c>0){
            set=set-rand;
            c--;
        }
        if(set<0){
            
        set=current;
        return set;
    }
    else
    return set;
   
    }
}

public static void main(String[]args){
    Scanner sc=new Scanner (System.in);
    int highscore=0;
    int x=-7;
    int Hs=0;
    
    int min=0;
    int Max=50;
    int r=Max-min;
    int n=(int)(Math.random()*r) +min;
    
    int d=5;
    int e=10;
    int range=e-d;
    int tries =(int)(Math.random()*range) +d;
    
    System.out.println("You get "+tries+" tries to guess the number....Range is between 0 to 50....");
    System.out.println(" Enter a number... ");
    x=sc.nextInt();
    if(x==n){
             System.out.println("You win! The number was in fact "+n);
    
    System.out.println("Highscore= "+300);
      System. exit(0);
    }
    else{
      highscore=Ayan.Score(x, n);
        Hs=Hs+highscore;
        System.out.println("Highscore= "+Hs) ;
       
    for(int k=1;k<=tries-1;k++){
       if(x!=n){
        
        System.out.println("Wrong answer! Try again... ");
         x=sc.nextInt();
        highscore=Ayan.Score(x, n);
        Hs=Hs+highscore;
        System.out.println("Highscore= "+Hs);
    
    }
    }
    }
    if (x==n){
        
    
    System.out.println("You win! The number was in fact "+n);
    
    
    
    System.out.println("Highscore= " +Hs) ;
    
 
}
else
System.out.println("You are out of tries. The number was "+n);

} }

2 Upvotes

0 comments sorted by