Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,149,728 members, 7,805,989 topics. Date: Tuesday, 23 April 2024 at 09:46 AM

Guess The Number - Java Codes - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Guess The Number - Java Codes (1138 Views)

Free Udemy Courses 100% Free [coupon Codes Applied] / How Do I Get Java Development Kit On My System? / Java EE Developer And Spring Developer In Here. (2) (3) (4)

(1) (Reply) (Go Down)

Guess The Number - Java Codes by ABCthingx: 3:56pm On Dec 17, 2019
Here is the code in java:
[b]


package com.sg;

import java.util.*;
public class GTN {
public static void main(String[] arrstring) {

int x = 0;

System.out.println("soloid game. "wink;
System.out.println("i pick a number from 1 to 100 then you guess it"wink;
Random random = new Random();
int Mg = random.nextInt(100);

Scanner scanner = new Scanner(System.in);
int Yg = 0;

while(Yg != Mg){

System.out.println("Your Guess"wink;
Yg = scanner.nextInt();
++x;
System.out.println(" "wink;
System.out.println(""wink;
System.out.println(String.valueOf((int)Yg) + " " + "it is"wink;

if (Yg == Mg) {
System.out.println(" correct! " + "you tried " + x + " times" );
break;
}
if (Mg < Yg) {
System.out.println("Too Big"wink;
//break loop;

}
if (Mg > Yg) {
System.out.println("Too Small"wink;
//break loop;
}

}
}
}



[/b]
Re: Guess The Number - Java Codes by ABCthingx: 4:00pm On Dec 17, 2019
I wanted to make it and app for android but developing with Java-NIDE is a pain in the ass. sad

So here is the J2ME version

For understand better you need knowledge of
1) Random
2) Alerts
3) Commands
4) Text Fields


[b]


import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;

public class GTN extends MIDlet implements CommandListener{
public Form form, ab;
public Display display;
public Image spl, th, tl, co, tk;
public Command ok, cancel1, quit;
public TextField text, tex;
public int G;
public int A;
public Command cancel = new Command("Cancel", Command.EXIT, 2);
public GTN() {
Random rand = new Random();
G = rand.nextInt(100);
form = new Form("Guess the number!"wink;
text = new TextField("I picked a number from 1 to 100, guess what it is.", "", 3, TextField.NUMERIC);

ok = new Command("OK", Command.OK, 2);
form.append(text);
form.addCommand(cancel);
form.addCommand(ok);
form.setCommandListener(this);

try{
spl = Image.createImage("/spl.png"wink;
th = Image.createImage("/TOOHIGH.png"wink;
tl = Image.createImage("/TOOLOW.png"wink;
co = Image.createImage("CORRECT.png"wink;
tk = Image.createImage("TK.png"wink;
}
catch(Exception e){
System.out.println(e.getMessage());
}
}

public void startApp(){
display = Display.getDisplay(this);
splash();
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
public void splash() {
Alert spl1 = new Alert("GUESS THE NUMBER!", "", spl, AlertType.INFO);
spl1.setImage(spl);
spl1.setTimeout(3000);
display.setCurrent(spl1, form);
}
public void about(){
ab = new Form("About"wink;
tex = new TextField("GTN is a project by ABCthingx www.nairaland.com/abcthingx", "", 25, TextField.ANY);
quit = new Command("quit", Command.OK, 2);
ab.append(tex);
try{
tk = Image.createImage("TK.png"wink;
}
catch(Exception e){
System.out.println(e.getMessage());
}
ab.append(tk);
ab.addCommand(quit);
ab.setCommandListener(this);
//display.setCurrent(ab);

}
public void greaterThan(){
Alert gt = new Alert("Try a lower number", "", th, AlertType.ERROR);
gt.setImage(th);
gt.setTimeout(3000);
display.setCurrent(gt, form);
}
public void lessThan(){
Alert lt = new Alert("TOO LOW", "try a higher number", tl, AlertType.ERROR);
lt.setImage(tl);
lt.setTimeout(3000);
display.setCurrent(lt, form);
}
public void equalTo(){
Alert et = new Alert("CORRECT!", "You tried " + A + " times", co, AlertType.ALARM);
et.setImage(co);
et.setTimeout(6000);
about(); //it seems this must be there or the form ab wouldn't run
display.setCurrent(et, ab);



}
public void controlHouse(int x ){
A += 1;
if (x != G ){
if (x < G){
lessThan();
}
if (x > G){
greaterThan();

}
}
else {
equalTo();

}


}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if(label.equals("Cancel"wink) {
destroyApp(true);
} else if(label.equals("OK"wink) {
controlHouse(Integer.parseInt(text.getString()));
}
else if(label.equals("quit"wink) {
destroyApp(true);
}
}
}

[/b]
Re: Guess The Number - Java Codes by ABCthingx: 4:08pm On Dec 17, 2019
I have the .jar and the source files in Zip but I can't upload it. Yes NL is that crappy.
Re: Guess The Number - Java Codes by progeek37(m): 4:22pm On Dec 17, 2019
Unfortunately Nairand is not suitable for writing programming code...you can benefit by writing it in an IDE and share the screenshots here. However from my eyes that code quality is poor, what is x, what is the meaning of GTN, what is Yg, Mg etc? All these will make your code hard to maintain by other programmers. Even if you revisit the code after few months, you will crack your brain to understand it.
Re: Guess The Number - Java Codes by Nobody: 4:25pm On Dec 17, 2019
I understand what the first code intends to do, you can easily make it Android app na. The game is a simple game though, how do you intend to make it look in Android? may be I could be of help?
Re: Guess The Number - Java Codes by Nobody: 4:27pm On Dec 17, 2019
progeek37:
Unfortunately Nairand is not suitable for writing programming code...you can benefit by writing it in an IDE and share the screenshots here. However from my eyes that code quality is poor, what is x, what is the meaning of GTN, what is Yg, Mg etc? All these will make your code hard to maintain by other programmers. Even if you revisit the code after few months, you will crack your brain to understand it.
he didn't follow the naming convention in Java I understand that.
Re: Guess The Number - Java Codes by ABCthingx: 5:01pm On Dec 17, 2019
progeek37:
Unfortunately Nairand is not suitable for writing programming code...you can benefit by writing it in an IDE and share the screenshots here. However from my eyes that code quality is poor, what is x, what is the meaning of GTN, what is Yg, Mg etc? All these will make your code hard to maintain by other programmers. Even if you revisit the code after few months, you will crack your brain to understand it.
If I share only the screenshots here. Anyone who wants to have a quick look at what the code does will first have to go through the pain of writing it from scratch again.

And yeah, the first code was as a result of try and error. Hopefully I will try to make it better later. smiley
Re: Guess The Number - Java Codes by ABCthingx: 5:15pm On Dec 17, 2019
2kaybiel:
I understand what the first code intends to do, you can easily make it Android app na. The game is a simple game though, how do you intend to make it look in Android? may be I could be of help?
Thanks, Here is a to that thread:
https://www.nairaland.com/5500875/want-make-simple-app

I couldn't go beyond the splash screen.
In Java-NIDE Once you make the second activity it see the compiler permanently saves that activity for you forever l, you can't change it and the activity is still there even when you delete it. Also on resources any resource that is not added on first compile can never be added again.
What a wicked world undecided
Re: Guess The Number - Java Codes by ABCthingx: 5:24pm On Dec 17, 2019
Incase you're interested in building the midlet here are the images I used.

If you noticed I used an alert for the splash screen I cannot come and kill myself. grin

Re: Guess The Number - Java Codes by ABCthingx: 5:28pm On Dec 17, 2019
And for the last part the about "TK.png" image
sorry but you have to get you own pic.
Re: Guess The Number - Java Codes by Nobody: 7:13pm On Dec 17, 2019
ABCthingx:
Thanks, Here is a to that thread:
https://www.nairaland.com/5500875/want-make-simple-app

I couldn't go beyond the splash screen.
In Java-NIDE Once you make the second activity it see the compiler permanently saves that activity for you forever l, you can't change it and the activity is still there even when you delete it. Also on resources any resource that is not added on first compile can never be added again.
What a wicked world undecided
then try AIDE app
Re: Guess The Number - Java Codes by Nobody: 7:42pm On Dec 17, 2019
Recently, I thought of learning another language, Java, I bought a textbook and downloaded PDF, the textbook made clear that I can download the JDK 8 while the PDF says I can Download the JDK 7, I went to Oracle, just to find out the latest JDK is JDK13, probably the the textbook and PDF was released many years before the latest JDK13 came out, is it possible to still use the same textbook and PDF to manage the JDK13, or should I Just download old JDK 7 or JDK 8, and use the follow the textbook. I haven't seen any latest textbook so far.
Re: Guess The Number - Java Codes by ABCthingx: 10:55pm On Dec 17, 2019
2kaybiel:
then try AIDE app
I once downloaded it. It is a commercial app. sad
Re: Guess The Number - Java Codes by crunchyDope(m): 5:30pm On Dec 18, 2019
Snowale:
Recently, I thought of learning another language, Java, I bought a textbook and downloaded PDF, the textbook made clear that I can download the JDK 8 while the PDF says I can Download the JDK 7, I went to Oracle, just to find out the latest JDK is JDK13, probably the the textbook and PDF was released many years before the latest JDK13 came out, is it possible to still use the same textbook and PDF to manage the JDK13, or should I Just download old JDK 7 or JDK 8, and use the follow the textbook. I haven't seen any latest textbook so far.
Lol! yeah
Re: Guess The Number - Java Codes by Nobody: 1:21pm On Dec 19, 2019
Snowale:
Recently, I thought of learning another language, Java, I bought a textbook and downloaded PDF, the textbook made clear that I can download the JDK 8 while the PDF says I can Download the JDK 7, I went to Oracle, just to find out the latest JDK is JDK13, probably the the textbook and PDF was released many years before the latest JDK13 came out, is it possible to still use the same textbook and PDF to manage the JDK13, or should I Just download old JDK 7 or JDK 8, and use the follow the textbook. I haven't seen any latest textbook so far.

JDK 14 will be released soon if it has not been released sef. Oracle releases JDk version every 6month.

Those textbook are your guidance you should follow up with the textbook steadily.
Re: Guess The Number - Java Codes by etoluw: 9:46am On Dec 20, 2019
ABCthingx:
I once downloaded it. It is a commercial app. sad

no it's free
it is something i use regularly
Re: Guess The Number - Java Codes by ABCthingx: 10:11am On Dec 20, 2019
etoluw:


no it's free
it is something i use regularly
Okay thanks I'll try it again.

(1) (Reply)

Mysql Or SQL Which Is The Best To Learn / How To Create A Screensaver With Visual Basic / I Want To Get Involved In Computer Programming, Where Do I Start?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 35
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.