• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assigning a variable

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.Scanner;
import java.util.Random;
class TestR {
public static void main(String args[]) {
Scanner myScanner=new Scanner(System.in);
int money=300;
int bet;
Random myRandom=new Random();
int randomNumber;
randomNumber=myRandom.nextInt(38)+1;
System.out.print("Pick a number between 1 and 38: ");
myScanner.nextDouble();<-- How do I assign a variable to this number? say "MyPick"
System.out.print("The number is ");
System.out.println(randomNumber);
System.out.print("You have ");
System.out.print(money);
System.out.println(" Dollars left");

}
}
Thanks,
Ron
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ron,

Welcome to the Ranch!

It's easier to read code snippets you post if you UseCodeTags (<-- click).

Also, it's better to keep your question separate from the code snippet so people aren't forced to read through all the code to find the question. The nice thing with code tags is that your code snippets will have line numbers. Before posting your message, you can click the Preview button, then reference the specific line(s) in question by Line number(s).

As for your question, it doesn't really make sense: you can't assign a variable (myPick) to a method (nextDouble()). Rather, you assign a value to a variable. I suspect that what you really want is to assign the value returned by myScanner.nextDouble() to a variable, myPick. If this is the case, you'd do the same thing you did as with randomNumber two lines back from the line you pointed out, with myPick on the left-hand side of the assignment operator and myScanner.nextDouble() on the right-hand side.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron,

Did you mean "How do I assign the value myScanner.nextDouble() returns to a variable"?

float myPick = myScanner.nextDouble();
 
Ron Larson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thank you.
reply
    Bookmark Topic Watch Topic
  • New Topic