Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I want to Make a game Enter ATM pin with only 5 chances if you guess the correct pin it will stop automatic, and if Chances = 0 it will stop also,
int answer, chances = 5;
do{
Scanner scan = new Scanner(System.in);
System.out.print("Enter your Bank ATM pin : ");
answer= scan.nextInt();
chances--;
}while(chances > 0);
}
You can do something like that:
int answer, chances = 5;
boolean authorized=false
do{
Scanner scan = new Scanner(System.in);
System.out.print("Enter your Bank ATM pin : ");
answer= scan.nextInt();
if(answer.equals("ok")){
authorized=true;
}else{
chances--;
}
}while(chances > 0 || authorized);
Code Safe
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
Improve this question
public static String reqnumber() {
Scanner keyboard = new Scanner(System.in);
String s;
boolean isMatch = true;
ArrayList<String>patientsNumbers = new ArrayList<String>();
int i = patientsNumbers.size();
do
{
System.out.println("enter number: ");
s = keyboard.next();
if(!(patientsNumbers.contains(s))){
patientsNumbers.add(s);
isMatch = false;
}else{
System.out.println("this is a number taken by another patient");
}
}while(isMatch);
return patientsNumbers.get(i);
}
here is a piece of code that includes an input validation loop, as every number of patient entered by user it should be saved in an arraylist called patientsNumbers.
and when the method reqnumber() is called in main, in the output when a user inputs a number already exist it should prompt the user that the number already taken by another patient and to enter another number but it doesn't, how to solve this!?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Here is the code I have.
//input
System.out.println("Using numbers, in what month were you born? ");
String **userMonth** = input.nextLine();
// also tried int **userMonth** = input.nextInt();
It does not work either way.
userMonth will not "activate?" (sorry noob and don't know all terms)
When tried to call later in program error:
if (userMonth < 3) {
System.out.println("Your vacation home is a van down by a river!");
}
//userMonth cannot be resolved to variable
Very odd that your variables are showing up as **varName** instead of varName.
First use:
int userMonth = 0; // initialize variable.
userMonth = input.nextInt();
But it does not consume the newline ('ENTER').
Therefore, immediately after, consume the newline character via:
input.nextLine();
Also make sure Scanner was declared as
Scanner input = new Scanner(System.in);
Don't forget to close the scanner at the end of program
input.close();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Example we input 20182391, how to just calculate the last digit of number? like "add the last digit with 2"
Thank you
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int lastDigit = input % 10;
//Do whatever you want to.
public static void main(String[] args){
Integer input = 20182391;
System.out.println(input % 10 + 2);
}
This should work
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I write a program where I need to take user input (prices) and giving the sum of all prices as output. Naturally prices can't be negative, so my question is: How does the program accept positive numbers only?
You want something like this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a price: ");
double number = input.nextDouble();
while (number < 0 ) {
System.out.print("Sorry, but your price must be a positive decimal. Enter a price: ");
number = input.nextDouble();
}
System.out.println("Your price is " + number);
}
}
Use a while loop to keep re-checking if the entered price meets your standards.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Scanner scanner = new Scanner(System.in);
System.out.println("1. Do you like computers?");
String input = scanner.nextLine();
if (input.equals("no")) {
System.out.println("You should!");
if (input.equals("yes")){
System.out.println("I like computers too!");
If the user inputs anything else than "yes" or "no", how would i repeat the question til the user inputs "yes" or "no". I am new to java so detailed explanations will help.
Going off of the code nhouser9 provided (which is case-sensitive), one way to check for all variants ("Yes", "YES", "yes") is to use the .toUpperCase() method.
String input = "";
while (!input.toUpperCase().equals("YES") && !input.toUpperCase().equals("NO")) {
input = scanner.nextLine();
}
String input = "";
while (!input.equals("Yes") && !input.equals("No")) {
input = scanner.nextLine();
}
The above should loop until the user has entered a valid input. Note that the input here is case sensitive - you can add more ifs to check for other cases if you need to.