Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

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

 
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:

 
Ranch Hand
Posts: 539
1
  • 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: 539
1
  • 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: 539
1
  • 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?
 
Ranch Hand
Posts: 64
4
Eclipse IDE C++ 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
Eclipse IDE C++ 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
Eclipse IDE C++ 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
Eclipse IDE C++ 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!
 
Greenhorn
Posts: 22
Eclipse IDE Chrome 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
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic