The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with string/characters

2 replies on 1 page. Most recent reply: Feb 17, 2003 6:34 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
AgentCyan

Posts: 9
Nickname: sulfricyan
Registered: Feb, 2003

Help with string/characters Posted: Feb 10, 2003 4:17 PM
Reply to this message Reply
Advertisement
Im required to make a hi/low game for my java class.

So far its been going well except we have never been taught how to get charater/sting/int inputs.

Im not even sure where to start, i dont know how to prompt the user for an answer.

If anyone can help me or give a link to code of a game such as this that would be great!

Thanks,

SuLfRiCyAn


Steve Austin

Posts: 1
Nickname: austin436
Registered: Feb, 2003

Re: Help with string/characters Posted: Feb 17, 2003 9:30 AM
Reply to this message Reply
System.out.println ("Enter an answer: ");

BufferedReader bufReader = new BufferedReader (new InputStreamReader(System.in));
String input = new String ( bufReader.readLine() );

That prompts for answer, and reads it. You just need to figure out what to do next...

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Help with string/characters Posted: Feb 17, 2003 6:34 PM
Reply to this message Reply

import java.util.*;

public class NumberGuess {

int answer;
boolean success;
String hint;
int numGuesses;

public NumberGuessBean() {
reset();
}

public void setGuess(String guess) {
numGuesses++;

int g;
try {
g = Integer.parseInt(guess);
}
catch (NumberFormatException e) {
g = -1;
}

if (g == answer) {
success = true;
}
else if (g == -1) {
hint = "a number next time";
}
else if (g < answer) {
hint = "higher";
}
else if (g > answer) {
hint = "lower";
}
}

public boolean getSuccess() {
return success;
}

public String getHint() {
return "" + hint;
}

public int getNumGuesses() {
return numGuesses;
}

public void reset() {
answer = Math.abs(new Random().nextInt() % 100) + 1;
success = false;
numGuesses = 0;
}
}

Flat View: This topic has 2 replies on 1 page
Topic: Create a session by JSP Previous Topic   Next Topic Topic: Graphics  NullPointerException

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use