Skip to main content

Help With Arrays/Math in Java

1 reply [Last post]
fenderguy440
Offline
Joined: 2011-03-22
Points: 0

Now, on to the project. Remember the grain of rice story? The raja gave out 1 grain of rice on the first day and doubled every day for 30 days. Well, we are going to change the story a bit.
§ Your program will ask the user how many grains of rice they get on the first day.
o Note: rice will double every day just like before.
§ Your program will then ask the user for how many days they get rice.

I have two problems with this project
1) I don't know how to use exponents in Java. I believe my equation is mathematically correct but the answer is not coming out correctly
2) I dont know how to use arrays entirely so I cant print out the number of rice I Will be getting on all days.
thanks in advance
here is the equation:

java.util.Scanner;
class myown{
public static void main(String args[]){
Scanner Andrew = new Scanner(System.in);
int rice, days, answer;
System.out.println("Enter how many pieces of rice you begin with: ");
rice = Andrew.nextInt();
System.out.println("Enter how many days you will receive rice for: ");
days = Andrew.nextInt();
answer = 2rice^(days-1) ;
System.out.println(answer);
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
OneJumpedUp
Offline
Joined: 2012-04-05
Points: 0

Your trying to overcomplicate something simple look at this code:

    public static void main(String[] args) 
    {
        Scanner s = new Scanner( System.in );
        System.out.print( "Enter no of Rice: " );
        int ans1 = s.nextInt();
       
        System.out.print( "Enter no of Days: " );
        int ans2 = s.nextInt();
        int rice = ans1;
       
        for( int nLoop = 0; nLoop < ans2; nLoop++ )
        {
            System.out.printf( "On day %d, he gets %d rice\n", nLoop + 1, rice );
            rice = rice * 2;
        }
    }