• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on how to request a user to input two numbers for "num1" and "num2" and do arithmetic.  RSS feed

 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, new to programming, and my teacher wants us to declare two variables : num1 and num2, and have the user input values, then run it through 2*num1 - num2.

Here is what I have so far, and I am lost and frustrated:

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dan,

Kindly use code tags when you post code here. It looks much better

Let's look at this line You need to receive an input from the user and assign it to num1, correct? Look at how you can do it using Scanner in this link.

you should be able to figure it out. Let us know how you go.
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you for showing me this, I guess my greatest issue is that I have no idea how to use the code provided. As in, where should I put it in my program?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at how the example takes input from the user. Look at what the 2nd line does here. Your num1 is 'i' in this example.
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this worked when I ran it in Eclipse, but I can't get the program to use the inputed numbers in the equation "2*num1 - num2".

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what you're doing in this line of code. The nextInt() method is used to receive an integer input from the user. Do you need an input from the user to compute your answer?
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I don't need an input. I need the program to take the inputed values of num1 and num2 and calculate them. How should I go about doing that?
 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Good wrote:You're right, I don't need an input. I need the program to take the inputed values of num1 and num2 and calculate them. How should I go about doing that?

Simply do System.out.println(2 * num1 - num2); or make answer equal (2 * num1 - num2) first and then System.out.println(); it out
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! It worked!

Final:

 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you being taught at the moment? I'm curious to what different programming courses start with.
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inputs, Outputs, decrements, increments, class string, at Fairfield University. We use the book Java Programming: From Problem Analysis to Program Design. The book is difficult to follow if you know nothing about coding...
 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. If the book gets too clunky at times or if you ever have any questions, we're here to help. \ ^ o ^ /
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This forum is great, thank you guys so much!
 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm kinda new to the forums, too
Nice place, I must admit.
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers mate!
 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Good wrote:Inputs, Outputs, decrements, increments, class string, at Fairfield University. We use the book Java Programming: From Problem Analysis to Program Design. The book is difficult to follow if you know nothing about coding...


well for beginners Head first java is the best book so far
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!