0

I’m trying to make a calculator that takes a number from the user then multiplies it I set but I’ve run into a roadblock. I need have my code take the product at the end of each step then multiply it by the numbers I set but I can’t figure out how to do that. All I’ve managed to do so far is multiply the user’s number at each step which isn’t what I need to do.

Example: User enters 8. My code takes the 8 and multiplies it by a number I set, in this case 6. I then need to take the product 48 and multiply it by another of my numbers, then take that product multiply it again, take that product and so on for several steps. I know I shouldn’t have entered userNum each time but I’m not sure what I’m supposed to put and I wanted to make sure the code at least partially functional. :P If it’s confusing, let me know and I’ll edit it.

    int firstNum;
    firstNum = 6;

    int secondNum;
    secondNum = 60;



    Scanner userInputScanner = new Scanner(System.in);

    System.out.println ("Hello, my name is Ronnie. What is your name?");

    String userName = userInputScanner.nextLine(); 

    System.out.println ("Hello, " + userName +  ". How many steps do you take in a ten second interval?"); 

    String userNumString = userInputScanner.nextLine();

    int userNum = Integer.parseInt(userNumString);      

    System.out.println("If you take " + userNum +  " steps in a 10 second interval, you could potentially achieve..."); 

    System.out.println ("Number of steps per minute: " + (userNum * firstNum) +""); 

    System.out.println ("Number of steps per hour: " + (userNum * secondNum) +""); 
2
  • 1
    You obviously need to use a loop here. Commented Sep 23, 2015 at 16:46
  • how many number you will have in your program and what is the max limit for userNum. Commented Sep 23, 2015 at 16:49

4 Answers 4

1

Use intermediate variables:

System.out.println("If you take " + userNum +  " steps in a 10 second " + 
    "interval, you could potentially achieve..."); 

long perMinute = userNum * 6;
System.out.println("Number of steps per minute: " + perMinute); 


long perHour = perMinute * 60;
System.out.println("Number of steps per hour: " + perHour); 

// ...
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this if your only doing two steps. otherwise you can create an intermediate variable product and save your result in it every time before printing.

System.out.println ("Number of steps per hour: " + (userNum *firstNum* secondNum) +"");

Comments

0

If you know it will always be just like this, you could just calculate the math in to each of these parts:

int userNum = Integer.parseInt(userNumString);      

System.out.println("If you take " + userNum +  " steps in a 10 second interval, you could potentially achieve..."); 

System.out.println ("Number of steps per minute: " + (userNum * firstNum) +""); 

System.out.println ("Number of steps per hour: " + (userNum * secondNum * firstNum) +""); 

Comments

0

As far as I could tell you want to record each multiplied value...

So, suppose you want 10 values: You could use an array:

int[] numbersToMultiply= [1,2,3,4,5,6,7,8,9,60];
int array[10];
Scanner userInputScanner = new Scanner(System.in);
System.out.println ("Hello, my name is Ronnie. What is your name?");
String userName = userInputScanner.nextLine();
System.out.println ("Hello, " + userName +  ". How many steps do you take in a ten second interval?");
String userNumString = userInputScanner.nextLine();

for(int i=0;i<10;i++){
   array[i] = userNum * numbersToMultiply[i];
}
System.out.println("If you take " + array[0] +  " steps in a 10 second interval, you could potentially achieve..."); 
System.out.println ("Number of steps per minute: " + array[1] +""); 

System.out.println ("Number of steps per hour: " array[10]""); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.