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 7 years ago.
Improve this question
I want to enter two numbers CSV and CR.
The condition is that:
If CSV is Positive, CR can be Positive or Negative.
If CSV is Negative, CR cannot be Positive
So how can i do this?
Scanner scanner = new Scanner(System.in);
System.out.print("CSV: ");
double csv = scanner.nextDouble();
System.out.print("CR: ");
double cr = scanner.nextDouble();
if (csv < 0.0) {
while (cr > 0.0) {
System.out.println("CR cannot be positive since CSV is negative.");
System.out.print("CR: ");
cr = scanner.nextDouble();
}
}
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 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 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 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 5 years ago.
Improve this question
I'm having trouble with an exercise.
The instructions are:
Create a program that asks the user for the first number and the last number and then prints all numbers between those two.
Below is my code. If the first number is greater than the last number, the program prints nothing.
System.out.println("First: ");
int first = Integer.parseInt(reader.nextLine());
System.out.println("Second: ");
int second = Integer.parseInt(reader.nextLine());
int number = first;
while (first <= second) {
System.out.println(number);
number = first + 1;
}
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 6 years ago.
Improve this question
Enter any number of integer values in a single row separated by space and the calculate and print the sum to next line.
EX: input: 1 2 3 4
output: 10
This should work - console.hasNext uses whitespace as its delimiter.
Scanner console = new Scanner(System.in);
int sum = 0;
while (console.hasNext()) {
sum += console.nextInt();
}
System.out.print(sum);
You can read the whole line as a String and than split it into a String array and than you can add it one by one for this purpose as following:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int sum=0;
for(int i=0;i<s.length;i++){
sum+=Integer.parseInt(s[i]);
}
System.out.println(sum);
for this you have to import :
import java.io.BufferedReader;
import java.io.InputStreamReader;
This is faster than Scanner, see this question for more details about Scanner and BufferedReader.