Java Saved Program: peaceful-terrain

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


import java.util.*;
import java.lang.*;
import java.io.*;
 
import java.util.Scanner;

class Script
{
    public static final Random rndGenerator = new Random();
	public static void main (String[] args)
	{
	    Scanner s = new Scanner(System.in);
        System.out.println("Pogodite broj od 1 do 20!");
	    int counter=0;
	    
	    boolean isGuessed=false;
	    for(int i=0; i<10; i++)
	    {
	    RndNumber numero = new RndNumber(rndGenerator);
        numero.generateNumber();
   
	    int x=s.nextInt();
	    System.out.println("Random broj: " + numero.getNumber());
	    System.out.println("Vas broj: " + x);
	    counter++;
	    if(x==numero.getNumber())
	        {
	        isGuessed=true;
	        System.out.println("Pogodili ste u " + counter + " pokusaja");
	        break;
	        }
	    }
	 Point points = new Point();
	 points.scorePoints(counter, isGuessed);
	 System.out.println("Postigli ste: " + points.getPoint() + " bodova!");

	}
}
class RndNumber
{
    private int _number;
    private Random _rndGenerator;
    public RndNumber(Random rndGenerator)
    {
        this._rndGenerator=rndGenerator;
        this._number=0;
    }
    public RndNumber(int number, Random rndGenerator)
    {
        this._rndGenerator=rndGenerator;
        this._number=number;
    }
    public void setNumber(int number)
    {
        this._number=number;
    }
    public int getNumber()
    {
        return this._number;
    }
    public void generateNumber()
    {
        int lb=1;
        int ub=20;
        this._number = lb + this._rndGenerator.nextInt(ub);
    }
}
class Point
{
    private int _point;
    public Point()
    {
        this._point=0;
    }
    public Point(int point)
    {
        this._point=point;
    }
    public void setPoint(int point)
    {
        this._point=point;
    }
    public int getPoint()
    {
        return this._point;
    }
    public void scorePoints(int counter, boolean isGuessed)
    {
           if(counter==10 &&isGuessed==false)
	    {
	        this._point=0;
	    }
	    else if(counter==10&&isGuessed==true)
	    {
	        this._point=1;
	    }
	    else if(counter<10&&counter>5)
	    {
	        this._point=2;
	    }
	    else if(counter<=5&&counter>=3)
	    {
	        this._point=3;
	    }
	    else if(counter==2)
	    {
	        this._point=4;
	    }
	    else if(counter==1)
	    {
	        this._point=5;
	    }
    }
}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs