• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assigning a variable  RSS feed

 
Ron Larson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Got it, thank you.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!