This is probably a easy question for most of you but the answer has evaded me for the most part. I'm writing a program to sort three numbers from lowest to highest and in the command prompt the inputs must be all on one line. I have the program working but for whatever reason I cannot get the inputs to show on one line. Instead I get something like:
Please enter three numbers: 1
2
3
Sorted numbers are: 1, 2, 3
Where it should show
Please enter three numbers: 1 2 3
Sorted numbers are: 1, 2, 3
My code:
import java.util.Scanner;
public class Ch5PA1
{
public static void main(String[] args) {
// Declarations
Scanner input = new Scanner(System.in);
System.out.print("Enter three values: ");
double num1 = input.nextDouble();
double num2 = input.nextDouble();
double num3 = input.nextDouble();
displaySortedNumbers(num1, num2, num3);
}
/** Sort Numbers */
public static void displaySortedNumbers(double num1, double num2, double num3){
double highest = num1 > num2 && num1 > num3 ? num1 : num2 > num1 && num2 > num3 ? num2 : num3;
double lowest = num1 < num2 && num1 < num3 ? num1 : num2 < num1 && num2 < num3 ? num2 : num3;
double middle = num1 != highest && num1 != lowest ? num1 : num2 != highest && num2 != lowest ? num2 : num3;
System.out.println("The sorted numbers are " + lowest + " " + middle + " " + highest);
}
}
You can take input from user like provide 3 numbers in comma seperated or space seperated. And split the string into array.
You don't need to change your code. Just separate your doubles with whitespace. (Spaces)
Related
My new problem is that myMin is equalling the last distance before the numbers are equal instead of the actual minimum. e.g. say the first two numbers I enter are 1 and 2, and the next are 1 and 3, and then 1 and 1. It is saying my minimum is 2.0. This is what I'm supposed to get for the assignment.
Enter number 1: 9
Enter number 2: 1
Enter number 1: 7
Enter number 2: 2
Enter number 1: 4
Enter number 2: 4
The minimum distance is: 5.0.
Enter number 1: 20
Enter number 2: 3
Enter number 1: 23
Enter number 2: 23
5.0 + 17.0 = 22.0
MY CODE:
double myMin = Double.MAX_VALUE;
double Min1,Min2;
while ( !(num1==num2) ) {
pairsMin( num1, num2, myMin);
Min1 = pairsMin( num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n\n");
myMin = Double.MAX_VALUE;
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
while ( !(num1==num2)) {
pairsMin( num1, num2, myMin);
Min2 = pairsMin(num1,num2,myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if(num1==num2) {
double totMin = Min1+Min2;
System.out.print("\n" + Min1 + " + " + Min2 + " = " + totMin + "\n");
}
}
}
} // end while loop
} // end main method
public static double pairsMin( double num1, double num2, double myMin){
double dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}
Change the two lines
pairsMin( num1, num2, myMin);
to
myMin = pairsMin( num1, num2, myMin);
At the moment you are always comparing to Double.MAX_VALUE and not to the new minimum value.
I am creating a program that will calculate two numbers. My issue is 0 will be an illegal input to the program but instead of asking again for two numbers.
The program continues to run without giving an error, or giving any answer when ZERO is imputed, it's suppose to ask the user to input two different numbers again.
I've done all the code and it mostly works and there is no visible error.
import java.util.*;
import java.util.Scanner.*;
public class MinilabLoopLogic
{
public static void main(String[ ] args)
{
int num1, num2, divisor, total;
Scanner kb = new Scanner(System.in); //you do it!
System.out.print("Please enter 2 integers (separated by spaces): ");
num1 = kb.nextInt();
num2 = kb.nextInt();
System.out.println("\n\nThis program will generate numbers BETWEEN "+ num1 + " " + num2);
System.out.println("\nPlease enter the integer your output should be divisible by: ");
divisor = kb.nextInt();
while (divisor == 0)
{
divisor = kb.nextInt();
}
System.out.println("\n\n----------------------------------------");
//Be able to handle 1st number smaller
// OR 2nd number smalle
//Use the modulus operator to check if a number is divisible
if (num1 < num2)
{
for (total = num1+1; total < num2; total++ )
{
if (total % divisor == 0)
{
System.out.println(total);
}
}
}
else
{
for (total = num1 - 1; total > num2; total--)
{
if (total % divisor == 0)
{
System.out.println(total);
}
}
}
}
}//end main()
Your problem lies in your while loop looking for the divisor:
System.out.println("\nPlease enter the integer your output should be divisible by: ");
divisor = kb.nextInt(); // gets your divisor
while (divisor == 0) //if it is illegal, e.g. 0
{
divisor = kb.nextInt(); // waits for more divisor input
}
You should output a string to tell the user what the problem is. say:
divisor = kb.nextInt(); // gets your divisor
while (divisor == 0) //if it is illegal, e.g. 0
{
System.out.println("\nYou can't divide by 0. Try again!: ");
divisor = kb.nextInt(); // waits for more divisor input
}
Also, if you want them to have to re-enter the num1 and num2 then scanner input has to happen in that while loop.
You added the following in an edit:
The program continues to run without giving an error, or giving any answer when ZERO is imputed, it's suppose to ask the user to input two different numbers again.
That is because you just loop back to get another value when a zero is entered, without writing any new prompt text.
It also only gets a new divisor number, not "two different numbers". If you looped back to before "Please enter 2 integers", it'd actually end up prompting for all 3 numbers again.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have been trying to make a basic calculator that calculates the mean of 9 numbers. The problem is it always skips the last line.
My code:
/* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package avarage.calc;
import java.util.Scanner;
/**
*
* #author taine
*/
public class AvarageCalc {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
double num7;
double num8;
double num9;
double num10;
double ans;
System.out.print("Enter Number #1:");
num1 = input.nextDouble();
System.out.print("Enter Number #2:");
num2 = input.nextDouble();
System.out.print("Enter Number #3:");
num3 = input.nextDouble();
System.out.print("Enter Number #4:");
num4 = input.nextDouble();
System.out.print("Enter Number #5:");
num5 = input.nextDouble();
System.out.print("Enter Number #6:");
num6 = input.nextDouble();
System.out.print("Enter Number #7:");
num7 = input.nextDouble();
System.out.print("Enter Number #8:");
num8 = input.nextDouble();
System.out.print("Enter Number #9:");
num9 = input.nextDouble();
ans = num1 + num2 + num3 + num4 + num5 + num6 + num8 + num9;
System.out.println("The Average of the numbers you gave is:" + ans / 9);
}
}
When the program runs:
run:
Enter Number #1:20
Enter Number #2:20
Enter Number #3:20
Enter Number #4:20
Enter Number #5:20
Enter Number #6:20
Enter Number #7:20
Enter Number #8:20
Enter Number #9:20
The Average of the numbers you gave is:17.77777777777778
BUILD SUCCESSFUL (total time: 10 seconds)
Your average is incorrect because you are missing num7 in your sum
ans = num1 + num2 + num3 + num4 + num5 + num6 + num8 + num9;
should be
ans = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9;
You have 10 variables, but you calculate average of 9. Is it what you want? Anyway, #the-tom has showed your mistake. You've forgot about num7 variable.
The less lines of code you have the less possibility to get an error.
Probably will be better to do something like that:
double ans = 0;
Scanner input = new Scanner(System.in);
int x = 9 //Or some other number
for(int i = 1; i <= x; i++){
System.out.print("Enter Number #" + i);
ans += input.nextDouble();
}
System.out.println("The Average of the numbers you gave is:" + ans / x);
Now if you need to calculate average of 20 elements, all you need is just set the value of x to 20.
I ran into a little problem here. Basically my program below is just generating two random numbers and divide both of them.
Before that I insert a statement whereby if num1 is not divisible by num2, then num2 has to generate a number between 1 to "num1" until it is divisible.
But in the end it keeps giving me a number which is not divisible (or basically giving decimal points). I tried looking for an example in the Internet and understood so well with the modulus operator. Where did I go wrong here? I just want both numbers to be divisible that's all.
Below is my code:
int num1, num2, real_ans;
Random randomGenerator = new Random();
num1 = randomGenerator.nextInt(100) + 1;
num2 = randomGenerator.nextInt(100) + 1;
if (num1%num2!=0) {
do {
num2 = randomGenerator.nextInt(num1) + 1
} while(num1%num2==0);
}
real_ans = num1 / num2;
Change the do/while loop to:
do{
num2 = randomGenerator.nextInt(num1) + 1
} while(num1 % num2 != 0);
(note the !=).
This loops until the numbers divide exactly.
you are iterating with the condition that if num1 is divisible by num2, keep iterating. Change the condition to do{...}while (num1%num2 != 0);.
You should be able to find a number that can divide num1 with that code. The only case it still won't work is when num1 is prime, so I suggest you check if num1 is prime and only generate num2, when num1 is not prime.
Write a program that uses a while loop to perform the following steps:
Comment by labelling each part: //Part A, //Part B, etc...
A.)Prompt the user to input 2 integers: firstNum and secondNum. Use 10 and 20.
B.)Output all odd numbers between firstNum and secondNum.
C.)Output the sum of all even numbers between firstNum and secondNum.
D.)Output the numbers and their square between 1 and 10.
E.)Output the sum of the square of odd numbers between firstNum and secondNum.
F.)Output all uppercase letters.
Again I am new to while loops and I am totally lost. I have tried reading a bunch but I am better at learning if someone shows me how to do something. I am open to all suggestions. I have just completed 8 other programs using if else statements and now trying to get the hang of loops. Thanks!
This is my pathetic attempt so far LOL
import java.util.Scanner;
public class whileLoop
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
//Part A
int firstNum = 10;
int secondNum = 20;
System.out.println("Please enter two integers: ");
int oddNum = keyboard.nextInt();
//Part B
while(firstNum <= secondNum)
{
if(firstNum % 2 != 0)
}
oddNum = firstNum + secondNum;
firstNum++;
}
System.out.println(""+oddNum);
}
}
I would like to show you how to use while loop on
Output all odd numbers between firstNum and secondNum.
Output the sum of all even numbers between firstNum and secondNum.
Output the sum of the square of odd numbers between firstNum and secondNum.
int i=firstnumber;
int sum=0;
int sumofsqr=0;
while(i<=secondnumber){
if(i%2==0){
System.out.println(i);
sum+=i;
}
else{
sumofsqr+=i*i;
}
i++;
}
System.out.println("Sum of odd " + sum + " Sum of sqr of even " + sumofsqr);
I think you meant to do this:
//Part B
int temp = firstNum;
while(temp <= secondNum)
{
if(temp % 2 != 0)
{
System.out.println(""+temp);
}
temp++;
}