- Joined
- Mar 22, 2022
- Posts
- 526
- Reaction
- 33
- Points
- 475
Guys pede makahingi ng codes dito, using inheritance need ko lang tlga nahihirapan na kasi ako jdoodle lang gamit ko. Salamat mga lods
Eto po yung code ko kaso di ko paalam paano gawin inheritance po ito jdoodle lang po kasi gamit ko.
import java.util.*;
class OutOfRangeException extends Exception{
public OutOfRangeException(){
super("Out of range\nGuess a number from 1 to 50!");
}
}
public class main1{
static void game(int guess, int target){}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Random r = new Random();
int target = r.nextInt((50 - 1) + 1) + 1;
int guess = -1;
System.out.println("Guess a number from 1 to 50!");
int count=1;
do{
try{
guess = sc.nextInt();
if(guess<1 || guess>50)
throw new OutOfRangeException();
if(guess<target){
System.out.println("Too low. Try again.");
count+=1;
}
if(guess>target){
System.out.println("Too high. Try again.");
count+=1;
}
}
catch(OutOfRangeException e){
System.out.println(e.getMessage());
count+=1;
}
catch(InputMismatchException e){
System.out.println("Invalid Input\nGuess a number from 1 to 50!");
count+=1;
sc.nextLine();
}
}while(guess!=target);
}
}
Eto po yung code ko kaso di ko paalam paano gawin inheritance po ito jdoodle lang po kasi gamit ko.
import java.util.*;
class OutOfRangeException extends Exception{
public OutOfRangeException(){
super("Out of range\nGuess a number from 1 to 50!");
}
}
public class main1{
static void game(int guess, int target){}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Random r = new Random();
int target = r.nextInt((50 - 1) + 1) + 1;
int guess = -1;
System.out.println("Guess a number from 1 to 50!");
int count=1;
do{
try{
guess = sc.nextInt();
if(guess<1 || guess>50)
throw new OutOfRangeException();
if(guess<target){
System.out.println("Too low. Try again.");
count+=1;
}
if(guess>target){
System.out.println("Too high. Try again.");
count+=1;
}
}
catch(OutOfRangeException e){
System.out.println(e.getMessage());
count+=1;
}
catch(InputMismatchException e){
System.out.println("Invalid Input\nGuess a number from 1 to 50!");
count+=1;
sc.nextLine();
}
}while(guess!=target);
}
}
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.
Last edited: