Click here to Skip to main content
16,007,687 members
Home / Discussions / Java
   

Java

 
GeneralRe: abt java project Pin
Cedric Moonen24-Oct-10 20:41
Cedric Moonen24-Oct-10 20:41 
GeneralRe: abt java project Pin
_Erik_25-Oct-10 4:58
_Erik_25-Oct-10 4:58 
Questionjava stacks Pin
Sylvane23-Oct-10 4:18
Sylvane23-Oct-10 4:18 
AnswerRe: java stacks Pin
Richard MacCutchan23-Oct-10 21:47
mveRichard MacCutchan23-Oct-10 21:47 
QuestionJTabel Cell Pin
trioum22-Oct-10 19:53
trioum22-Oct-10 19:53 
AnswerRe: JTabel Cell Pin
David Skelly24-Oct-10 22:16
David Skelly24-Oct-10 22:16 
QuestionNeed help with this code [modified] Pin
zooropean720-Oct-10 16:33
zooropean720-Oct-10 16:33 
AnswerRe: Need help with this code Pin
Bugclear20-Oct-10 21:32
Bugclear20-Oct-10 21:32 
Hey ur program had a couple of bugs after i removed the null pointer error. Fixed it Smile | :)

Code
-------------------------------------
import java.util.*;

public class Guess
{
public static void main (String[] args)
{
final int MAX = 100;
int answer, guess, attempt = 0;
String replay = "Y";

Scanner scan = new Scanner (System.in);
Random generator = new Random();
answer = generator.nextInt(MAX) + 1;
System.out.print("I am thinking of a number "
+ "between 1 and 100. Enter your guess (0 to guit):");
guess = scan.nextInt();

do
{
if(guess == 101){

answer = generator.nextInt(MAX) + 1;

System.out.print("I am thinking of a number "
+ "between 1 and 100. Enter your guess (0 to guit):");
guess = scan.nextInt();
}

if (guess == 0)
{
System.out.println ("You ended the game. Play again? (y/n):");
replay = scan.next();
guess = 101;
attempt = 0;
}
else{
if (guess > answer)
{
attempt++;
System.out.print ("Try a smaller number (0 to quit): ");
guess = scan.nextInt();
}
if (guess < answer)
{
attempt++;
System.out.print ("Try a bigger number (0 to quit): ");
guess = scan.nextInt();
}
}

if (guess == answer)
{
attempt++;
System.out.println ("You got it!");
System.out.println ("Number of total attempts:" + attempt);
System.out.println ();
System.out.print("Play again? (y/n)");
replay = scan.next();
guess = 101;
attempt = 0;

}

}while (replay.equals("y") || replay.equals("Y"));



}
}
GeneralRe: Need help with this code Pin
Richard MacCutchan20-Oct-10 22:10
mveRichard MacCutchan20-Oct-10 22:10 
GeneralRe: Need help with this code Pin
zooropean721-Oct-10 8:33
zooropean721-Oct-10 8:33 
GeneralRe: Need help with this code Pin
Richard MacCutchan21-Oct-10 11:21
mveRichard MacCutchan21-Oct-10 11:21 
GeneralRe: Need help with this code Pin
zooropean721-Oct-10 12:16
zooropean721-Oct-10 12:16 
GeneralRe: Need help with this code Pin
Bugclear21-Oct-10 20:31
Bugclear21-Oct-10 20:31 
GeneralRe: Need help with this code Pin
TorstenH.21-Oct-10 22:52
TorstenH.21-Oct-10 22:52 
GeneralRe: Need help with this code Pin
Bugclear21-Oct-10 23:22
Bugclear21-Oct-10 23:22 
GeneralRe: Need help with this code Pin
Richard MacCutchan22-Oct-10 3:05
mveRichard MacCutchan22-Oct-10 3:05 
QuestionException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 0:19
vidhyaej20-Oct-10 0:19 
AnswerRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
TorstenH.20-Oct-10 0:56
TorstenH.20-Oct-10 0:56 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 1:03
vidhyaej20-Oct-10 1:03 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
TorstenH.20-Oct-10 1:19
TorstenH.20-Oct-10 1:19 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 1:29
vidhyaej20-Oct-10 1:29 
QuestionClasses and methods Pin
vidhyaej19-Oct-10 21:28
vidhyaej19-Oct-10 21:28 
AnswerRe: Classes and methods Pin
Nagy Vilmos19-Oct-10 22:01
professionalNagy Vilmos19-Oct-10 22:01 
GeneralRe: Classes and methods Pin
vidhyaej19-Oct-10 22:58
vidhyaej19-Oct-10 22:58 
GeneralRe: Classes and methods Pin
Richard MacCutchan19-Oct-10 23:23
mveRichard MacCutchan19-Oct-10 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.