Get more lessons like this at www.MathTutorDV... Learn how to use the scanner() method to read integers, doubles, and floats in from the keyboard.
Пікірлер: 15
@raymondRodriguez2 жыл бұрын
I find it much easier this way public class Age { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int age, newAge; System.out.println("Enter your age: "); age = scan.nextInt(); newAge = age + 14; System.out.println("You are now " + age); System.out.println("In 14 years, you will be " +newAge+ " years old."); newAge = scan.nextInt(); } }
@joeroganpodfantasy422 жыл бұрын
All looks good but curious why add the last line newAge = scan.nextInt(); ?
@nicolasjoye52395 жыл бұрын
Hi After ENTERING the age, I have to press another time, otherwise the "You are now 10 years... " is not appearing. In the video/excecrcise before there was the same issue. Why do I have to double-ENTER? Thank you
@joeroganpodfantasy422 жыл бұрын
it's a you problem
@SirFency5 жыл бұрын
why dont this work? do you have to formatt hasNextInt() if statement exactly like you have it? if(input.hasNextInt() == true && age > 1){ age = input.nextInt(); System.out.println("You are currently " +age+ " years old, in 14 years you will be "+(age+14)+" years old."); }
@张慧盈-g9x5 жыл бұрын
.
@张慧盈-g9x5 жыл бұрын
If you assign age to 1, then : if(input.hasNextInt() == true && age >=1) //You need to put equal sign, then both side will evaluate to TRUE, your code will execute.
@joeroganpodfantasy422 жыл бұрын
You haven't declared the variable age, You haven't initialized the variable age, If you initialize it to 1 it still will not print because the if statement is valued as false because 1 > 1 is false. You either make it age 2 or more or add ">=" or remove it completely cause it's redundant code there is no reason for it.
@hendrecarstens54824 жыл бұрын
public static void main(String[] args) { Scanner userInput = new Scanner(System.in); int age; int years = 14; System.out.println("Please enter your age: "); if (userInput.hasNextInt() == true) { age = userInput.nextInt(); age+=years; //( age = age + 14) => this is optional. I used 'age+=years' within System.out.println() System.out.println("You are now " + age + " years old."); System.out.println("In " + years + " years you will be " + (age+=years) + " years old."); } } // Note: Notice the curly brackets used after " if (userInput.hasNextInt() == true) { }" If your script is added within these there will be no error if the expected 'int" input for "age = userInput.nextInt();" is not a valid integer. Your program will just stop. You also don't need to declare age=1 if your code is within these brackets. If you don't use these { } brackets, java will say: " variable may not have been initialized"
@joeroganpodfantasy422 жыл бұрын
Interesting findings from your note. EXPANDING ON WHY FROM WHAT WE HAVE LEARNED SO FAR: If variables are not initialized and they are used later in the code it will produce an error. But if they are inside the scope of an if statement , it will be as if they don't exist and code will run just fine. So you either declare them inside the if statement and never initialize them manually with dummy values. or if you declare them outside in the outer scope make sure to never mention them in the outer scope , you can mention them without error in the inner scope of an if statement.
@paincodename2 жыл бұрын
Here it's my version :P Scanner input = new Scanner(System.in); int age, ageAfter14Years = 14, answer = 0; age = input.nextInt(); System.out.println("You are now: " + age + " Years old"); answer = ageAfter14Years + age; System.out.println("In the 14 years, you will be: " + answer + " Years old"); answer = input.nextInt();
@joeroganpodfantasy422 жыл бұрын
you are overcomplicating a simple +14 addition. But I don't understand why did you add the last line: answer = input.nextInt(); what does it do from your perspective?
@paincodename2 жыл бұрын
@@joeroganpodfantasy42 in this case it doesn't do anything, it just stays there and waits, because i had to code some more code and try a few other things
@vonnuevon86003 жыл бұрын
my solution , with if functions that outputs an error if the user did not type an integer or a whole number. import java.util.Scanner; public class lesson9 { public static void main(String[] args) { Scanner object1 = new Scanner(System.in); int arf,meow; System.out.println("Enter your age:"); if(object1.hasNextInt()==true) { arf = object1.nextInt(); meow= arf+14; System.out.println("you are now "+arf+" years old" + " in 14 years , you will be "+meow+" years old");}; if(object1.hasNextInt()!=true) { System.out.println("Error: Please Type Integer/Whole Number"); }; } }
@joeroganpodfantasy422 жыл бұрын
oh the arf meow guy again you write the messiest most interesting bizarre code I have ever seen. 1. the extra semicolon after the if statement is done is not necessary but your code still works somehow , instead of writing just false you do the opposite keep true the same and change to a not statement != I don't know to be fascinated or horrified by your code. But I guess it's just another POV , it's interesting. You either are gifted for code or you should never write code again but I don't know which :D