Allowing user to input variable.
Hello, I'm new to programming, and could use a little help with something!
I feel like I'm missing the obvious answer, but I just cant think of it!
I'm trying to allow the user to input 5 variables when the program prompts them, and then these variables will be used later on as the number amount of fabricated items that the user has to use.
For Example:
"Hello! Input the number of Arrows you want!"
"=user inputs #="
"You have # arrows! How many arrows would you like to use?"
"user inputs #"
I need the number the user inputted to be a variable, so that the user cant use more of the item than they inputed that they had.
I'm sorry for posting no code, but I don't see how posting it here would help with what I need to know.
Thanks!
Re: Allowing user to input variable.
Quote:
There are many ways to skin a cat!
Check out this:
Java Tips - How to read input from console
Otherwise, if you wanted something bigger/permanently store values and go beyond the basics, then I suggest reading a Java programming book or online resources, and generate something amazing.
Hope this helps.
Re: Allowing user to input variable.
Re: Allowing user to input variable.
Quote:
Originally Posted by
That1guy
Hello, I'm new to programming, and could use a little help with something!
I feel like I'm missing the obvious answer, but I just cant think of it!
I'm trying to allow the user to input 5 variables when the program prompts them, and then these variables will be used later on as the number amount of fabricated items that the user has to use.
For Example:
"Hello! Input the number of Arrows you want!"
"=user inputs #="
"You have # arrows! How many arrows would you like to use?"
"user inputs #"
I need the number the user inputted to be a variable, so that the user cant use more of the item than they inputed that they had.
I'm sorry for posting no code, but I don't see how posting it here would help with what I need to know.
Thanks!
Scanner console = new Scanner(System.in);
int numOfArrows = 0;
int numberOfArrowsToUse = 0;
System.out.println("Hello! Input the number of Arrows you want!");
numOfArrows = console.nextInt();
System.out.println("You have " + numOfArrows + " arrows! How many arrows would you like to use?");
numOfArrowsToUse = console.nextInt();
However, if you're using JOptionPane, then
int numOfArrows = 0;
int numberOfArrowsToUse = 0;
// JOptionPane.showInputDialog( Component parent, String message, Object title, int messageType);
String str = JOptionPane.showInputDialog(null, "Hello! Input the number of arrows you want!", "Input number of arrows", JOptionPane.QUESTION_MESSAGE);
numOfArrows = Integer.parseInt(str);
String str2 = JOptionPane.showInputDialog(null, "You have " + numOfArrows + " arrows! How many arrows would you like to use?", "How many do you want to use?", JOptionPane.QUESTION_MESSAGE);
numberOfArrowsToUse = Integer.parseInt(str2);
Re: Allowing user to input variable.
Thank you all!
I used the Arrow simulation provided by javapenguin above; and after a little tweaking, got it to run perfectly. I then used that to start working on what I actually have in mind; but when compling it, I keep running into errors; and I cant quite figure out what I have wrong.
Code :
import java.util.Scanner;
class Cafe
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int mealA = 0;
int mealB = 0;
int mealC = 0;
int mealD = 0;
int mealE = 0;
int mealtoeat = 0;
System.out.println("Please tell me what first food you want to make!");
foodA = scan.nextLine();
System.out.println("Please tell me what second food you want to make!");
foodB = scan.nextLine();
System.out.println("Please tell me what third food you want to make!");
foodC = scan.nextLine();
System.out.println("Please tell me what fourth food you want to make!");
foodD = scan.nextLine();
System.out.println("Please tell me what fifth food you want to make!");
foodE = scan.nextLine();
System.out.println("How many " + foodA + " do you want to make?");
meal1 = scan.nextInt();
System.out.println("How many " + foodB + " do you want to make?");
meal2 = scan.nextInt();
System.out.println("How many " + foodC + " do you want to make?");
meal3 = scan.nextInt();
System.out.println("How many " + foodD + " do you want to make?");
meal4 = scan.nextInt();
System.out.println("How many " + foodE + " do you want to make?");
meal5 = scan.nextInt();
{
System.out.println(foodA + " " + mealA);
System.out.println(foodB + " " + mealB);
System.out.println(foodC + " " + mealC);
System.out.println(foodD + " " + mealD);
System.out.println(foodE + " " + mealE);
System.out.println("What would you like to eat?");
//How do I make sure that the user can only enter one of the foods they chose to make?
System.out.println("You have " + //??? + " of " + //??? + "! How many
+ //??? + " would you like to eat?");
mealtoeat = scan.nextInt();
}
}
}
I got alot of errors around the "How many ___ do you want to make?" parts of it.
Also, as you can see by my notes left in, I cant quite figure how to make sure that the user can only input the foods they chose to make. If someone could direct me to somplace where I can learn more about that; it would be much appriciated.
Thanks again!
Re: Allowing user to input variable.
get rid of the set of brackets that start after meal5 = scan.nextInt() and that end after mealtoeat = scan.nextInt(), those aren't needed and will cause errors.
try adding
Code java:
String foodA = " ";
String foodB = " ";
String foodC = " ";
String foodD = " ";
String foodE = " ";
int mealtoeat2 = 0;
and
int meal1 = 0;
int meal2 = 0;
int meal3 = 0;
int meal4 = 0;
int meal5 = 0;
int choice =0;
System.out.println("Choose the meal you want to eat. 1 = " + foodlA + " 2 = " + foodlB + " 3 = " + foodC + " 4 = " + foodD + " 5 = " + foodE + ".");
boolean isAValidMeal = false;
while (isAValidMeal ==false)
{
mealtoeat = scan.nextInt();
if (mealtoeat==1)
{
what you'll do when it's 1
isAValidMeal = true;
}
else if (mealtoeat ==2)
{
what you'll do when it's 2
isAValidMeal = true;
}
else if (mealtoeat ==3)
{
what you'll do when it's 3
isAValidMeal = true;
}
else if (mealtoeat ==4)
{
what you'll do when it's 4
isAValidMeal = true;
}
else if (mealtoeat ==5)
{
what you'll do when it's 5
isAValidMeal =true;
}
else
{
System.out.println("Enter a value between 1 and 5.");
isAValidMeal = false;
}
}
I'm assuming you're asking how many of a certain meal type of a certain food you'd like to eat.
If so, within each if and else if statement, have, for example, for the first if statement
Code java:
boolean isAValidNumber = false;
while (isAValidNumber == false)
{
System.out.println("You have " + meal1 + " of " + mealA + "! How many " + mealA + " would you like to eat?");
mealtoeat2 = console.nextInt();
if (mealtoeat2 > 0 && mealtoeat2 <= meal1)
{
it works and do what you have to do
isAValidNumber = true;
}
else
{
System.out.println("Enter a number at least 0 and at most the number of meals you made for that type.");
isAValidNumber = false;
}
}
You'll have to do something similar for the other ones.