How to build my own Java Math library?

Java code help please?

  • So it works... labels change and everything... but I will guess "8" and it says too low, then I guess "9" and it says too high... Is my random number coming out to be a decimal? Also, my "attempt" label does not update itself... how do i fix that? import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class HiLow extends Applet implements ActionListener { private Panel nPnl; //Declaring Variables private Panel sPnl; private Panel cPnl; private Label HiLow[]; private TextField type; private Button button; private int count = 1; private int guess; private double number; public void init() //Start to work on the Applet { type = new TextField(""); nPnl = new Panel(); sPnl = new Panel(); cPnl = new Panel(); HiLow = new Label[5]; button = new Button(String.valueOf("Submit Guess")); number = (Math.random()*25); HiLow[0] = new Label(" "); //Labels HiLow[1] = new Label(" "); HiLow[2] = new Label("WELCOME TO HI/LOW!"); HiLow[3] = new Label("Attempt #" + count); HiLow[4] = new Label("Guess a number between 0 and 25 below."); setLayout(new BorderLayout()); nPnl.setLayout (new GridLayout(2,1)); //Set the Dimensions of the Grids sPnl.setLayout (new GridLayout(2,1)); cPnl.setLayout (new GridLayout(3,1)); nPnl.add(HiLow[2]); //Build the Panels nPnl.add(HiLow[4]); cPnl.add(HiLow[0]); cPnl.add(HiLow[1]); cPnl.add(type); sPnl.add(button); sPnl.add(HiLow[3]); setBackground(Color.green); add(nPnl, BorderLayout.NORTH); //Add Panels to Layout add(sPnl, BorderLayout.SOUTH); add(cPnl, BorderLayout.CENTER); button.addActionListener(this); } public void actionPerformed(ActionEvent e) { guess = Integer.parseInt(type.getText()); count++; while (guess != number) { if (guess > number) { HiLow[0].setText("Sorry, Too High!"); HiLow[1].setText(""); guess = Integer.parseInt(type.getText()); } else if (guess < number) { HiLow[0].setText(""); HiLow[1].setText("Sorry, Too Low!"); guess = Integer.parseInt(type.getText()); } else if (guess > 25 && guess < 0) { HiLow[0].setText("That number is not in range."); HiLow[1].setText("Please guess a number between 0-25."); guess = Integer.parseInt(type.getText()); } else if (guess == number) { HiLow[0].setText("Congradulations!")… HiLow[1].setText("You Guessed Correctly!"); } break; } } }

  • Answer:

    This is the problem....number = (Math.random()*25); It generates a random number between 0 and 1 and multiplies by 25. Easy fix though, just cast it to an int int number = (int)(Math.random()*25);

kevinm27... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.