Very new to Java basic question
import java.util.Scanner;
public class IfTest0
{
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
//Prompt Hits
System.out.print( "Enter number of hits > " );
int hitAmount = scan.nextInt();
System.out.println( "Number of hits is " + hitAmount );
//Promt at bats
System.out.print( "Enter number of at bats > " );
int atBats = scan.nextInt();
System.out.println( "Number of at bats is " + atBats );
int Average;
Average = (hitAmount/atBats);
if ( Average>0.300 )
{
System.out.println( "Eligible for All-Stars " );
}
else
{
System.out.println ( "Not eligible for All-Stars " );
}
}
}
So I'm trying to use an if/else statement to state that when the batting average is greater than .300 the player is eligible for all stars. What happens is that when I input the hits and at bats it always says the player is not eligible. But when the average equals one the player is eligible which I find very strange but I can't figure out what is wrong with my code.
Edit: I think I figured it out, I was using the / to divide which would give me a value of 0 I just need to figure out how to properly divide now I'm assuming.
Re: Very new to Java basic question
Do you know about integer division? 4/5 = 0 and 10/8 = 1
If you want fractional values you should use double variables.
Re: Very new to Java basic question
Quote:
Originally Posted by
Norm
Do you know about integer division? 4/5 = 0 and 10/8 = 1
If you want fractional values you should use double variables.
That worked perfectly I changed my int's to doubles and now I'm getting the correct response.
One more question on the other program I have:
public class IfTest1
{
public static void main(String[] args)
{
double square;
double rectangle;
double SquareSide;
double RectangleSide1;
double RectangleSide2;
SquareSide = 0.666666667;
RectangleSide1 = 1/9;
RectangleSide2 = 4;
square = SquareSide*SquareSide;
System.out.println ( "The area of the square is " + square);
rectangle = RectangleSide1*RectangleSide2;
System.out.println ( "The area of the rectangle is " + rectangle);
This is where I assumed my problem was the division, I know that my 1/9 value is giving me 0. What do I need to do in order to get a decimal value?
Re: Very new to Java basic question
Change either the 1 or 9 to a double. For example: 1.0/9