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.
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 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 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
I want to write A program To check and Print Out If The Number Is Odd Or Even.
This Is What I Have Programmed So Far:
import java.util.Scanner;
public class Irs_Lab
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter an integer :: ");
int num = kb.nextInt();
}
}
I Heard You can Use A % (mod) to get the remainder of division.
Can anyone help me on this.
You can use the modular operator to see if a number is even or odd(as you said). When dividing a number by 2, if it is even it will have a remainder of 0, if it is odd it will have a remainder of 1. Using if(a % 2 == 0){//even}else{//odd} is your likely solution
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 5 years ago.
Improve this question
I've tried looking for the answer to this question, but I haven't had a lot of luck. Basically I am asked to create the Fibonacci Code, and then allow a user input to look for there input in the sequence. If its in the sequence then it shows what index. If its not then it shows the two closest numbers to it.
So if a user inputs 4, the nearest elements would be 3 and 5 and the indexes would be 4 and 5.
I'm basically struggling with finding the nearest elements. I'm not exactly sure how to do it.
****update****
So I did figure it out thank you
1.Store the previous Fibonacci number in a buffer (you can initialize with -1)
2.update the buffer after every new number is calculated.
3.if the current number is not equal to the new number
3.A check if the number is greater than buffer and less than the new number
3.A.1)If yes, those two are your nearest numbers.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int userInput;
Fibonacci.fibonacciAlgor(5);
}
public static void fibonacciAlgor(int userInput)
{
int i=0;
int buffer=-1;
int x=0,y=1;
System.out.println("Input: " + userInput);
while(i<1000000){
if(x==userInput){
System.out.println("Belongs to sequence: Yes "");
break;
}
else{
if(userInput>buffer&&userInput<x){
System.out.println("Belongs to sequence: No ");
System.out.println("Nearest Elements: " + buffer+","+x);
break;
}
}
buffer=x;
int temp=y;
y=x+y;
x=temp;
i++;
}
}
}
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 8 years ago.
Improve this question
I am trying to validate input values to pass only integers that are divisible by 10. The code below is failing.
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
ArrayList<Integer> liste = new ArrayList<Integer>(); // I have filled my array with integers
int x=scan.nextInt();
int y=x%10;
do{
if(y==0){
liste.add(x);}
else if(y!=0){
System.out.println("It is not valid"); continue;
}
else
{System.out.println("Enter only integer"); continue;
}
}while(scan.hasNextInt()); }
System.out.println(liste);
System.out.println("Your largest value of your arraylist is: "+max(liste));
You're calling scan.nextInt() twice. Each time you call it, it will read another int from the input. Thus, if your input was something like
10
5
13
then the 10 would pass the scan.nextInt()%10==0 check, and then 5 would be added to the list. Store the result of scan.nextInt() in a variable first, so the value won't change.
Instead of
if(scan.nextInt()%10==0){
liste.add(scan.nextInt());}
do
int num = scan.nextInt();
if(num%10 == 0){
liste.add(num);
}